home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / jikes-1.02 / src / body.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-26  |  96.0 KB  |  2,346 lines

  1. // $Id: body.cpp,v 1.21 1999/08/26 15:34:02 shields Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10.  
  11. #include "config.h"
  12. #include <assert.h>
  13. #include "semantic.h"
  14. #include "control.h"
  15.  
  16. void Semantic::ProcessBlockStatements(AstBlock *block_body)
  17. {
  18.     //
  19.     // An empty block that is not a switch block can complete normally
  20.     // iff it is reachable. A nonempty block that is not a switch
  21.     // block can complete normally iff the last statement in it can
  22.     // complete normally.
  23.     //
  24.     if (block_body -> NumStatements() == 0)
  25.          block_body -> can_complete_normally = block_body -> is_reachable;
  26.     else
  27.     {
  28.         //
  29.         // The first statement in a nonempty block that is not a
  30.         // switch block is reachable iff the block is reachable.
  31.         // Every other statement S in a nonempty block that is not a
  32.         // switch block is reachable iff the statement preceeding S
  33.         // can complete normally.
  34.         //
  35.         AstStatement *statement = (AstStatement *) block_body -> Statement(0);
  36.         statement -> is_reachable = block_body -> is_reachable;
  37.         AstStatement *first_unreachable_statement = (AstStatement *) (statement -> is_reachable ? NULL : statement);
  38.         ProcessStatement(statement);
  39.         for (int i = 1; i < block_body -> NumStatements(); i++)
  40.         {
  41.             AstStatement *previous_statement = statement;
  42.             statement = (AstStatement *) block_body -> Statement(i);
  43.             statement -> is_reachable = previous_statement -> can_complete_normally;
  44.             if (! statement -> is_reachable && (first_unreachable_statement == NULL))
  45.                 first_unreachable_statement = statement;
  46.  
  47.             ProcessStatement(statement);
  48.         }
  49.  
  50.         if (statement -> can_complete_normally)
  51.             block_body -> can_complete_normally = true;
  52.  
  53.         //
  54.         // If we have one or more unreachable statements that are contained in a
  55.         // reachable block then issue message. (If the enclosing block is not reachable
  56.         // the message will be issued later for the enclosing block.)
  57.         //
  58.         if (first_unreachable_statement && LocalBlockStack().TopBlock() -> is_reachable)
  59.         {
  60.             if (first_unreachable_statement == statement)
  61.             {
  62.                 ReportSemError(SemanticError::UNREACHABLE_STATEMENT,
  63.                                statement -> LeftToken(),
  64.                                statement -> RightToken());
  65.             }
  66.             else
  67.             {
  68.                 ReportSemError(SemanticError::UNREACHABLE_STATEMENTS,
  69.                                first_unreachable_statement -> LeftToken(),
  70.                                statement -> RightToken());
  71.             }
  72.         }
  73.  
  74.         //
  75.         // If an enclosed block has a higher max_variable_index than the current block,
  76.         // update max_variable_index in the current_block, accordingly.
  77.         //
  78.         BlockSymbol *block = block_body -> block_symbol;
  79.         if (block -> max_variable_index < LocalBlockStack().TopMaxEnclosedVariableIndex())
  80.             block -> max_variable_index = LocalBlockStack().TopMaxEnclosedVariableIndex();
  81.     }
  82.  
  83.     return;
  84. }
  85.  
  86.  
  87. void Semantic::ProcessBlock(Ast *stmt)
  88. {
  89.     AstBlock *block_body = (AstBlock *) stmt;
  90.  
  91.     AstBlock *enclosing_block = LocalBlockStack().TopBlock();
  92.  
  93.     //
  94.     // Guess that the number of elements will not exceed the number of statements + 3. The +3 takes into account
  95.     // one label + one ForInit declaration and one extra something else.
  96.     //
  97.     int table_size = block_body -> NumStatements() + 3;
  98.     BlockSymbol *block = LocalSymbolTable().Top() -> InsertBlockSymbol(table_size);
  99.     //
  100.     // enclosing_block is not present only when we are processing the block of a static initializer
  101.     //
  102.     block -> max_variable_index = (enclosing_block ? enclosing_block -> block_symbol -> max_variable_index : 1);
  103.     LocalSymbolTable().Push(block -> Table());
  104.  
  105.     block_body -> block_symbol = block;
  106.     block_body -> nesting_level = LocalBlockStack().Size();
  107.     LocalBlockStack().Push(block_body);
  108.  
  109.     //
  110.     // Note that in constructing the Ast, the parser encloses each
  111.     // labeled statement in its own block. Therefore the declaration
  112.     // of this label will not conflict with the declaration of another
  113.     // label with the same name declared at the same nesting level.
  114.     //
  115.     // For example, the following sequence of statements is legal:
  116.     //
  117.     //     l: a = b;
  118.     //     l: b = c;
  119.     //
  120.     for (int i = 0; i < block_body -> NumLabels(); i++)
  121.     {
  122.         NameSymbol *name_symbol = lex_stream -> NameSymbol(block_body -> Label(i));
  123.         Symbol *symbol = NULL;
  124.         for (SemanticEnvironment *env = state_stack.Top(); env; env = env -> previous)
  125.         {
  126.             symbol = env -> symbol_table.FindLabelSymbol(name_symbol);
  127.             if (symbol)
  128.                 break;
  129.         }
  130.  
  131.         if (symbol)
  132.         {
  133.             ReportSemError(SemanticError::DUPLICATE_LABEL,
  134.                            block_body -> Label(i),
  135.                            block_body -> Label(i),
  136.                            name_symbol -> Name());
  137.         }
  138.         else
  139.         {
  140.             LabelSymbol *label = LocalSymbolTable().Top() -> InsertLabelSymbol(name_symbol);
  141.             label -> block = block_body;
  142.             label -> nesting_level = block_body -> nesting_level;
  143.         }
  144.     }
  145.  
  146.     ProcessBlockStatements(block_body);
  147.  
  148.     LocalBlockStack().Pop();
  149.     LocalSymbolTable().Pop();
  150.  
  151.     //
  152.     // Update the information for the block that immediately encloses the current block.
  153.     //
  154.     if (enclosing_block)
  155.     {
  156.         if (LocalBlockStack().TopMaxEnclosedVariableIndex() < block -> max_variable_index)
  157.             LocalBlockStack().TopMaxEnclosedVariableIndex() = block -> max_variable_index;
  158.     }
  159.  
  160.     block -> CompressSpace(); // space optimization
  161.  
  162.     return;
  163. }
  164.  
  165.  
  166. void Semantic::ProcessLocalVariableDeclarationStatement(Ast *stmt)
  167. {
  168.     AstLocalVariableDeclarationStatement *local_decl = (AstLocalVariableDeclarationStatement *) stmt;
  169.  
  170.     AstArrayType *array_type = local_decl -> type -> ArrayTypeCast();
  171.     Ast *actual_type = (array_type ? array_type -> type : local_decl -> type);
  172.  
  173.     if ((! control.option.one_one) && local_decl -> NumLocalModifiers() > 0)
  174.     {
  175.         ReportSemError(SemanticError::ONE_ONE_FEATURE,
  176.                        local_decl -> LocalModifier(0) -> LeftToken(),
  177.                        local_decl -> LocalModifier(local_decl -> NumLocalModifiers() - 1) -> RightToken());
  178.     }
  179.     AccessFlags access_flags = ProcessLocalModifiers(local_decl);
  180.  
  181.     AstPrimitiveType *primitive_type = actual_type -> PrimitiveTypeCast();
  182.     TypeSymbol *field_type = (primitive_type ? field_type = FindPrimitiveType(primitive_type) : MustFindType(actual_type));
  183.  
  184.     for (int i = 0; i < local_decl -> NumVariableDeclarators(); i++)
  185.     {
  186.         AstVariableDeclarator *variable_declarator = local_decl -> VariableDeclarator(i);
  187.         AstVariableDeclaratorId *name = variable_declarator -> variable_declarator_name;
  188.         NameSymbol *name_symbol = lex_stream -> NameSymbol(name -> identifier_token);
  189.  
  190. //
  191. // TODO: Confirm that this new test is indeed the case. In 1.0, only the more restricted test below was necessary.
  192. //
  193. //        if (LocalSymbolTable().FindVariableSymbol(name_symbol))
  194. //        {
  195. //            ReportSemError(SemanticError::DUPLICATE_LOCAL_VARIABLE_DECLARATION,
  196. //                           name -> identifier_token,
  197. //                           name -> identifier_token,
  198. //                           name_symbol -> Name());
  199. //        }
  200. //
  201.         SemanticEnvironment *where_found;
  202.         Tuple<VariableSymbol *> variables_found(2);
  203.         SearchForVariableInEnvironment(variables_found, where_found, state_stack.Top(), name_symbol, name -> identifier_token);
  204.         VariableSymbol *symbol = (variables_found.Length() > 0 ? variables_found[0] : (VariableSymbol *) NULL);
  205.         if (symbol && symbol -> IsLocal())
  206.         {
  207.             ReportSemError(SemanticError::DUPLICATE_LOCAL_VARIABLE_DECLARATION,
  208.                            name -> identifier_token,
  209.                            name -> identifier_token,
  210.                            name_symbol -> Name());
  211.         }
  212.         else
  213.         {
  214.             symbol = LocalSymbolTable().Top() -> InsertVariableSymbol(name_symbol);
  215.             variable_declarator -> symbol = symbol;
  216.  
  217.             int num_dimensions = (array_type ? array_type -> NumBrackets() : 0) + name -> NumBrackets();
  218.             if (num_dimensions == 0)
  219.                  symbol -> SetType(field_type);
  220.             else symbol -> SetType(field_type -> GetArrayType((Semantic *) this, num_dimensions));
  221.             symbol -> SetFlags(access_flags);
  222.             symbol -> SetOwner(ThisMethod());
  223.             symbol -> declarator = variable_declarator;
  224.             BlockSymbol *block = LocalBlockStack().TopBlock() -> block_symbol;
  225.             symbol -> SetLocalVariableIndex(block -> max_variable_index++); // Assigning a local_variable_index to a variable
  226.                                                                             // also marks it complete as a side-effect.
  227.             if (control.IsDoubleWordType(symbol -> Type()))
  228.                 block -> max_variable_index++;
  229.  
  230.             if (variable_declarator -> variable_initializer_opt)
  231.                  ProcessVariableInitializer(variable_declarator);
  232.         }
  233.     }
  234.  
  235.     //
  236.     // A local variable declaration statement can complete normally
  237.     // iff it is reachable.
  238.     //
  239.     local_decl -> can_complete_normally = local_decl -> is_reachable;
  240.  
  241.     return;
  242. }
  243.  
  244.  
  245. void Semantic::ProcessExpressionStatement(Ast *stmt)
  246. {
  247.     AstExpressionStatement *expression_statement = (AstExpressionStatement *) stmt;
  248.  
  249.     ProcessExpression(expression_statement -> expression);
  250.  
  251.     //
  252.     // An expression statement can complete normally iff it is reachable.
  253.     //
  254.     expression_statement -> can_complete_normally = expression_statement -> is_reachable;
  255.  
  256.     return;
  257. }
  258.  
  259.  
  260. void Semantic::ProcessSynchronizedStatement(Ast *stmt)
  261. {
  262.     AstSynchronizedStatement *synchronized_statement = (AstSynchronizedStatement *) stmt;
  263.  
  264.     ProcessExpression(synchronized_statement -> expression);
  265.  
  266.     synchronized_statement -> block -> is_reachable = synchronized_statement -> is_reachable;
  267.  
  268.     if (synchronized_statement -> expression -> Type() -> Primitive())
  269.     {
  270.         ReportSemError(SemanticError::TYPE_NOT_REFERENCE,
  271.                        synchronized_statement -> expression -> LeftToken(),
  272.                        synchronized_statement -> expression -> RightToken(),
  273.                        synchronized_statement -> expression -> Type() -> Name());
  274.     }
  275.  
  276.     AstBlock *enclosing_block = LocalBlockStack().TopBlock(),
  277.              *block_body = synchronized_statement -> block;
  278.  
  279.     //
  280.     // Guess that the number of elements will not exceed the number of statements + 3.
  281.     //
  282.     BlockSymbol *block = LocalSymbolTable().Top() -> InsertBlockSymbol(block_body -> NumStatements() + 3);
  283.     //
  284.     // "synchronized" blocks require two more local variable slots for synchronization,
  285.     // plus one extra variable slot if the containing method returns a value, plus an
  286.     // additional slot if the value returned is double or long.
  287.     //
  288.     block -> synchronized_variable_index = enclosing_block -> block_symbol -> max_variable_index;
  289.     block -> max_variable_index = block -> synchronized_variable_index + 2;
  290.     if (control.IsDoubleWordType(ThisMethod() -> Type()))
  291.          block -> max_variable_index += 2;
  292.     else if (ThisMethod() -> Type() != control.void_type)
  293.          block -> max_variable_index++;
  294.  
  295.     LocalSymbolTable().Push(block -> Table());
  296.  
  297.     block_body -> block_symbol = block;
  298.     block_body -> nesting_level = LocalBlockStack().Size();
  299.     LocalBlockStack().Push(block_body);
  300.  
  301.     ProcessBlockStatements(block_body);
  302.  
  303.     LocalBlockStack().Pop();
  304.     LocalSymbolTable().Pop();
  305.  
  306.     if (LocalBlockStack().TopMaxEnclosedVariableIndex() < block -> max_variable_index)
  307.         LocalBlockStack().TopMaxEnclosedVariableIndex() = block -> max_variable_index;
  308.  
  309.     //
  310.     // If the synchronized_statement is enclosed in a loop and it contains a reachable continue statement
  311.     // then it may have already been marked as "can complete normally";
  312.     //
  313.     synchronized_statement -> can_complete_normally = synchronized_statement -> can_complete_normally ||
  314.                                                       synchronized_statement -> block -> can_complete_normally;
  315.  
  316.     block -> CompressSpace(); // space optimization
  317.  
  318.     return;
  319. }
  320.  
  321.  
  322. void Semantic::ProcessIfStatement(Ast *stmt)
  323. {
  324.     AstIfStatement *if_statement = (AstIfStatement *) stmt;
  325.  
  326.     ProcessExpression(if_statement -> expression);
  327.  
  328.     if (if_statement -> expression -> Type() != control.no_type &&
  329.         if_statement -> expression -> Type() != control.boolean_type)
  330.     {
  331.         ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  332.                        if_statement -> expression -> LeftToken(),
  333.                        if_statement -> expression -> RightToken(),
  334.                        if_statement -> expression -> Type() -> Name());
  335.     }
  336.  
  337.     //
  338.     // Recall that the parser ensures that the statements that appear in an if-statement
  339.     // (both the true and false statement) are enclosed in a block.
  340.     //
  341.     if_statement -> true_statement -> is_reachable = if_statement -> is_reachable;
  342.     ProcessBlock(if_statement -> true_statement);
  343.  
  344.     if (if_statement -> false_statement_opt)
  345.     {
  346.         if_statement -> false_statement_opt -> is_reachable = if_statement -> is_reachable;
  347.         ProcessBlock(if_statement -> false_statement_opt);
  348.  
  349.         //
  350.         // If the if_statement is enclosed in a loop and it contains a reachable continue statement
  351.         // then it may have already been marked as "can complete normally";
  352.         //
  353.         if_statement -> can_complete_normally = if_statement -> can_complete_normally ||
  354.                                                 if_statement -> true_statement -> can_complete_normally ||
  355.                                                 if_statement -> false_statement_opt -> can_complete_normally;
  356.     }
  357.     else if_statement -> can_complete_normally = if_statement -> is_reachable;
  358.  
  359.     return;
  360. }
  361.  
  362.  
  363. void Semantic::ProcessWhileStatement(Ast *stmt)
  364. {
  365.     AstWhileStatement *while_statement = (AstWhileStatement *) stmt;
  366.  
  367.     //
  368.     // Recall that each while statement is enclosed in a unique block by the parser
  369.     //
  370.     BreakableStatementStack().Push(LocalBlockStack().TopBlock());
  371.     ContinuableStatementStack().Push(LocalBlockStack().TopBlock());
  372.  
  373.     AstStatement *enclosed_statement = while_statement -> statement;
  374.     enclosed_statement -> is_reachable = while_statement -> is_reachable;
  375.  
  376.     ProcessExpression(while_statement -> expression);
  377.     if (while_statement -> expression -> Type() == control.boolean_type)
  378.     {
  379.         if (while_statement -> expression -> IsConstant())
  380.         {
  381.             IntLiteralValue *literal = (IntLiteralValue *) while_statement -> expression -> value;
  382.             if (! literal -> value)
  383.             {
  384.                  if (while_statement -> is_reachable)
  385.                      while_statement -> can_complete_normally = true;
  386.                  enclosed_statement -> is_reachable = false;
  387.             }
  388.         }
  389.         else if (while_statement -> is_reachable)
  390.              while_statement -> can_complete_normally = true;
  391.     }
  392.     else if (while_statement -> expression -> Type() != control.no_type)
  393.     {
  394.         ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  395.                        while_statement -> expression -> LeftToken(),
  396.                        while_statement -> expression -> RightToken(),
  397.                        while_statement -> expression -> Type() -> Name());
  398.     }
  399.  
  400.     ProcessStatement(enclosed_statement);
  401.  
  402.     if ((! enclosed_statement -> is_reachable) && (while_statement -> is_reachable))
  403.     {
  404.         ReportSemError(SemanticError::UNREACHABLE_STATEMENT,
  405.                        enclosed_statement -> LeftToken(),
  406.                        enclosed_statement -> RightToken());
  407.     }
  408.  
  409.     //
  410.     // If the while statement contained a reachable break statement,
  411.     // then the while statement can complete normally. It is marked
  412.     // here only for completeness, as marking the enclosing block is
  413.     // enough to propagate the proper information upward.
  414.     //
  415.     AstBlock *block_body = (AstBlock *) BreakableStatementStack().Top();
  416.     if (block_body -> can_complete_normally)
  417.         while_statement -> can_complete_normally = true;
  418.  
  419.     BreakableStatementStack().Pop();
  420.     ContinuableStatementStack().Pop();
  421.  
  422.     return;
  423. }
  424.  
  425.  
  426. void Semantic::ProcessForStatement(Ast *stmt)
  427. {
  428.     AstForStatement *for_statement = (AstForStatement *) stmt;
  429.  
  430.     //
  431.     // Note that in constructing the Ast, the parser encloses each
  432.     // for-statement whose for-init-statements starts with a local
  433.     // variable declaration in its own block. Therefore a redeclaration
  434.     // of another local variable with the same name in a different loop
  435.     // at the same nesting level will not cause any conflict.
  436.     //
  437.     // For example, the following sequence of statements is legal:
  438.     //
  439.     //     for (int i = 0; i < 10; i++);
  440.     //     for (int i = 10; i < 20; i++);
  441.     //
  442.     for (int i = 0; i < for_statement -> NumForInitStatements(); i++)
  443.         ProcessStatement(for_statement -> ForInitStatement(i));
  444.  
  445.     //
  446.     // Recall that each for statement is enclosed in a unique block by the parser
  447.     //
  448.     BreakableStatementStack().Push(LocalBlockStack().TopBlock());
  449.     ContinuableStatementStack().Push(LocalBlockStack().TopBlock());
  450.  
  451.     //
  452.     // Assume that if the for_statement is reachable then its
  453.     // contained statement is also reachable. If it turns out that the
  454.     // condition (end) expression is a constant FALSE expression we will
  455.     // change the assumption...
  456.     //
  457.     AstStatement *enclosed_statement = for_statement -> statement;
  458.     enclosed_statement -> is_reachable = for_statement -> is_reachable;
  459.  
  460.     if (for_statement -> end_expression_opt)
  461.     {
  462.         ProcessExpression(for_statement -> end_expression_opt);
  463.         if (for_statement -> end_expression_opt -> Type() == control.boolean_type)
  464.         {
  465.             if (for_statement -> end_expression_opt -> IsConstant())
  466.             {
  467.                 IntLiteralValue *literal = (IntLiteralValue *) for_statement -> end_expression_opt -> value;
  468.                 if (! literal -> value)
  469.                 {
  470.                      if (for_statement -> is_reachable)
  471.                          for_statement -> can_complete_normally = true;
  472.                      enclosed_statement -> is_reachable = false;
  473.                 }
  474.             }
  475.             else if (for_statement -> is_reachable)
  476.                  for_statement -> can_complete_normally = true;
  477.         }
  478.         else if (for_statement -> end_expression_opt -> Type() != control.no_type)
  479.         {
  480.             ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  481.                            for_statement -> end_expression_opt -> LeftToken(),
  482.                            for_statement -> end_expression_opt -> RightToken(),
  483.                            for_statement -> end_expression_opt -> Type() -> Name());
  484.         }
  485.     }
  486.  
  487.     ProcessStatement(enclosed_statement);
  488.  
  489.     if ((! enclosed_statement -> is_reachable) && (for_statement -> is_reachable))
  490.     {
  491.         ReportSemError(SemanticError::UNREACHABLE_STATEMENT,
  492.                        enclosed_statement -> LeftToken(),
  493.                        enclosed_statement -> RightToken());
  494.     }
  495.  
  496.     for (int j = 0; j < for_statement -> NumForUpdateStatements(); j++)
  497.         ProcessExpressionStatement(for_statement -> ForUpdateStatement(j));
  498.  
  499.     //
  500.     // If the for statement contained a reachable break statement,
  501.     // then the for statement can complete normally. It is marked
  502.     // here only for completeness, as marking the enclosing block is
  503.     // enough to propagate the proper information upward.
  504.     //
  505.     AstBlock *block_body = (AstBlock *) BreakableStatementStack().Top();
  506.     if (block_body -> can_complete_normally)
  507.         for_statement -> can_complete_normally = true;
  508.  
  509.     BreakableStatementStack().Pop();
  510.     ContinuableStatementStack().Pop();
  511.  
  512.     return;
  513. }
  514.  
  515.  
  516. void Semantic::ProcessSwitchStatement(Ast *stmt)
  517. {
  518.     AstSwitchStatement *switch_statement = (AstSwitchStatement *) stmt;
  519.  
  520.     AstBlock *enclosing_block = LocalBlockStack().TopBlock();
  521.  
  522.     //
  523.     // We estimate a size for the switch symbol table based on the number of lines in it.
  524.     //
  525.     AstBlock *block_body = switch_statement -> switch_block;
  526.     BlockSymbol *block = LocalSymbolTable().Top() -> InsertBlockSymbol();
  527.     block -> max_variable_index = enclosing_block -> block_symbol -> max_variable_index;
  528.     LocalSymbolTable().Push(block -> Table());
  529.  
  530.     block_body -> block_symbol = block;
  531.     block_body -> nesting_level = LocalBlockStack().Size();
  532.     LocalBlockStack().Push(block_body);
  533.     BreakableStatementStack().Push(block_body);
  534.  
  535.     ProcessExpression(switch_statement -> expression);
  536.     TypeSymbol *type = switch_statement -> expression -> Type();
  537.  
  538.     if (type != control.int_type  && type != control.short_type &&
  539.         type != control.char_type && type != control.byte_type  && type != control.no_type)
  540.     {
  541.         ReportSemError(SemanticError::TYPE_NOT_VALID_FOR_SWITCH,
  542.                        switch_statement -> expression -> LeftToken(),
  543.                        switch_statement -> expression -> RightToken(),
  544.                        type -> ContainingPackage() -> PackageName(),
  545.                        type -> ExternalName());
  546.     }
  547.  
  548.     switch_statement -> default_case.switch_block_statement = NULL;
  549.  
  550.     //
  551.     // Count the number case labels in this switch statement
  552.     //
  553.     int num_case_labels = 0;
  554.     for (int i = 0; i < block_body -> NumStatements(); i++)
  555.     {
  556.         AstSwitchBlockStatement *switch_block_statement = (AstSwitchBlockStatement *) block_body -> Statement(i);
  557.         num_case_labels += switch_block_statement -> NumSwitchLabels();
  558.     }
  559.     switch_statement -> AllocateCases(num_case_labels);
  560.  
  561.     //
  562.     // A switch block is reachable iff its switch statement is reachable.
  563.     //
  564.     block_body -> is_reachable = switch_statement -> is_reachable;
  565.     for (int j = 0; j < block_body -> NumStatements(); j++)
  566.     {
  567.         AstSwitchBlockStatement *switch_block_statement = (AstSwitchBlockStatement *) block_body -> Statement(j);
  568.  
  569.         for (int k = 0; k < switch_block_statement -> NumSwitchLabels(); k++)
  570.         {
  571.             AstCaseLabel *case_label = switch_block_statement -> SwitchLabel(k) -> CaseLabelCast();
  572.  
  573.             if (case_label)
  574.             {
  575.                 ProcessExpression(case_label -> expression);
  576.  
  577.                 if (case_label -> expression -> Type() != control.no_type)
  578.                 {
  579.                     if (! case_label -> expression -> IsConstant())
  580.                     {
  581.                         ReportSemError(SemanticError::EXPRESSION_NOT_CONSTANT,
  582.                                        case_label -> expression -> LeftToken(),
  583.                                        case_label -> expression -> RightToken());
  584.                         case_label -> expression -> symbol = control.no_type;
  585.                     }
  586.                     else if (type != control.no_type)
  587.                     {
  588.                         if (CanAssignmentConvert(type, case_label -> expression))
  589.                         {
  590.                             if (case_label -> expression -> Type() != type)
  591.                                 case_label -> expression = ConvertToType(case_label -> expression, type);
  592.  
  593.                             case_label -> map_index = switch_statement -> NumCases();
  594.  
  595.                             CaseElement *case_element = compilation_unit -> ast_pool -> GenCaseElement();
  596.                             switch_statement -> AddCase(case_element);
  597.  
  598.                             case_element -> expression = case_label -> expression;
  599.                             case_element -> switch_block_statement = switch_block_statement;
  600.                             case_element -> index = case_label -> map_index; // use this index to keep sort stable !
  601.                         }
  602.                         else if (case_label -> expression -> Type() == control.int_type &&
  603.                                  ! IsIntValueRepresentableInType(case_label -> expression, type))
  604.                         {
  605.                             IntToWstring value(((IntLiteralValue *) (case_label -> expression -> value)) -> value);
  606.  
  607.                             ReportSemError(SemanticError::VALUE_NOT_REPRESENTABLE_IN_SWITCH_TYPE,
  608.                                            case_label -> expression -> LeftToken(),
  609.                                            case_label -> expression -> RightToken(),
  610.                                            value.String(),
  611.                                            type -> Name());
  612.                         }
  613.                         else
  614.                         {
  615.                             ReportSemError(SemanticError::TYPE_NOT_CONVERTIBLE_TO_SWITCH_TYPE,
  616.                                            case_label -> expression -> LeftToken(),
  617.                                            case_label -> expression -> RightToken(),
  618.                                            case_label -> expression -> Type() -> Name(),
  619.                                            type -> Name());
  620.                         }
  621.                     }
  622.                 }
  623.             }
  624.             else if (switch_statement -> default_case.switch_block_statement == NULL)
  625.                 switch_statement -> default_case.switch_block_statement = switch_block_statement;
  626.             else
  627.             {
  628.                 ReportSemError(SemanticError::MULTIPLE_DEFAULT_LABEL,
  629.                                ((AstDefaultLabel *) switch_block_statement -> SwitchLabel(k)) -> LeftToken(),
  630.                                ((AstDefaultLabel *) switch_block_statement -> SwitchLabel(k)) -> RightToken());
  631.             }
  632.         }
  633.  
  634.         //
  635.         // The parser ensures that a switch block statement always has one statement.
  636.         // When a switch block ends with a sequence of switch labels that are not followed
  637.         // by any executable statements, an artificial "empty" statement is added by the parser.
  638.         //
  639.         assert(switch_block_statement -> NumStatements() > 0);
  640.  
  641.         //
  642.         // A statement in a switch block is reachable iff its
  643.         // switch statement is reachable and at least one of the
  644.         // following is true:
  645.         //
  646.         // . it bears a case or default label
  647.         // . there is a statement preceeding it in the switch block and that
  648.         // preceeding  statement can compile normally.
  649.         //
  650.         AstStatement *statement = (AstStatement *) switch_block_statement -> Statement(0);
  651.         statement -> is_reachable = switch_statement -> is_reachable;
  652.         AstStatement *first_unreachable_statement = (AstStatement *) (statement -> is_reachable ? NULL : statement);
  653.         ProcessStatement(statement);
  654.         for (int j = 1; j < switch_block_statement -> NumStatements(); j++)
  655.         {
  656.             AstStatement *previous_statement = statement;
  657.             statement = (AstStatement *) switch_block_statement -> Statement(j);
  658.             if (switch_statement -> is_reachable)
  659.             {
  660.                 statement -> is_reachable = previous_statement -> can_complete_normally;
  661.                 if ((! statement -> is_reachable) && (first_unreachable_statement == NULL))
  662.                     first_unreachable_statement = statement;
  663.             }
  664.  
  665.             ProcessStatement(statement);
  666.         }
  667.  
  668.         if (first_unreachable_statement)
  669.         {
  670.             if (first_unreachable_statement == statement)
  671.             {
  672.                 ReportSemError(SemanticError::UNREACHABLE_STATEMENT,
  673.                                statement -> LeftToken(),
  674.                                statement -> RightToken());
  675.             }
  676.             else
  677.             {
  678.                 ReportSemError(SemanticError::UNREACHABLE_STATEMENTS,
  679.                                first_unreachable_statement -> LeftToken(),
  680.                                statement -> RightToken());
  681.             }
  682.         }
  683.     }
  684.  
  685.     //
  686.     // A switch statement can complete normally iff at least one of the
  687.     // following is true:
  688.     //
  689.     // . there is a reachable break statement that exits the switch
  690.     //   statement. (See ProcessBreakStatement)
  691.     // . the switch block is empty or contains only switch labels
  692.     //   //
  693.     //   // TODO: This statement seems to be erroneous. The proper statement
  694.     //   //       as implemented here is:
  695.     //   //
  696.     //   //           . the switch block is empty or contains only case labels
  697.     //   //
  698.     // . there is at least one switch label after the last switch block
  699.     //   statement group.
  700.     // . the last statement in the switch block can complete normally
  701.     //
  702.     if (block_body -> can_complete_normally)
  703.         switch_statement -> can_complete_normally = true;
  704.     else if (switch_statement -> default_case.switch_block_statement == NULL)
  705.         switch_statement -> can_complete_normally = true;
  706.     else
  707.     {
  708.         AstSwitchBlockStatement *last_switch_block_statement = (AstSwitchBlockStatement *)
  709.                                                                block_body -> Statement(block_body -> NumStatements() - 1);
  710.  
  711.         assert(last_switch_block_statement -> NumStatements() > 0);
  712.  
  713.         AstStatement *last_statement = (AstStatement *)
  714.                                        last_switch_block_statement -> Statement(last_switch_block_statement -> NumStatements() - 1);
  715.         if (last_statement -> can_complete_normally)
  716.             switch_statement -> can_complete_normally = true;
  717.     }
  718.  
  719.     switch_statement -> SortCases();
  720.     for (int k = 1; k < switch_statement -> NumCases(); k++)
  721.     {
  722.         if (switch_statement -> Case(k) -> Value() == switch_statement -> Case(k - 1) -> Value())
  723.         {
  724.             IntToWstring value(switch_statement -> Case(k) -> Value());
  725.  
  726.             ReportSemError(SemanticError::DUPLICATE_CASE_VALUE,
  727.                            switch_statement -> Case(k) -> expression -> LeftToken(),
  728.                            switch_statement -> Case(k) -> expression -> RightToken(),
  729.                            value.String());
  730.         }
  731.     }
  732.  
  733.     //
  734.     // If an enclosed block has a higher max_variable_index than the current block,
  735.     // update max_variable_index in the current_block, accordingly.
  736.     // Also, update the information for the block that immediately encloses the current block.
  737.     //
  738.     if (block -> max_variable_index < LocalBlockStack().TopMaxEnclosedVariableIndex())
  739.         block -> max_variable_index = LocalBlockStack().TopMaxEnclosedVariableIndex();
  740.  
  741.     BreakableStatementStack().Pop();
  742.     LocalBlockStack().Pop();
  743.     LocalSymbolTable().Pop();
  744.  
  745.     if (enclosing_block)
  746.     {
  747.         if (LocalBlockStack().TopMaxEnclosedVariableIndex() < block -> max_variable_index)
  748.             LocalBlockStack().TopMaxEnclosedVariableIndex() = block -> max_variable_index;
  749.     }
  750.  
  751.     block -> CompressSpace(); // space optimization
  752.  
  753.     return;
  754. }
  755.  
  756.  
  757. void Semantic::ProcessDoStatement(Ast *stmt)
  758. {
  759.     AstDoStatement *do_statement = (AstDoStatement *) stmt;
  760.  
  761.     //
  762.     // Recall that each Do statement is enclosed in a unique block by the parser
  763.     //
  764.     BreakableStatementStack().Push(LocalBlockStack().TopBlock());
  765.     ContinuableStatementStack().Push(LocalBlockStack().TopBlock());
  766.  
  767.     AstStatement *enclosed_statement = do_statement -> statement;
  768.     enclosed_statement -> is_reachable = do_statement -> is_reachable;
  769.  
  770.     ProcessStatement(enclosed_statement);
  771.  
  772.     ProcessExpression(do_statement -> expression);
  773.  
  774.     IntLiteralValue *literal = NULL;
  775.     if (do_statement -> expression -> Type() == control.boolean_type)
  776.     {
  777.         if (do_statement -> expression -> IsConstant())
  778.             literal = (IntLiteralValue *) do_statement -> expression -> value;
  779.     }
  780.     else if (do_statement -> expression -> Type() != control.no_type)
  781.     {
  782.         ReportSemError(SemanticError::TYPE_NOT_BOOLEAN,
  783.                        do_statement -> expression -> LeftToken(),
  784.                        do_statement -> expression -> RightToken(),
  785.                        do_statement -> expression -> Type() -> Name());
  786.     }
  787.  
  788.     //
  789.     // A do statement can complete normally, iff at least one of the following is true:
  790.     //     1. The contained statement can complete normally and the condition expression
  791.     //        is not a constant expression with the value true
  792.     //     2. There is a reachable break statement that exits the do statement
  793.     //        (This condition is true is the block that immediately encloses this do statement
  794.     //         can complete normally. See ProcessBreakStatement)
  795.     //
  796.     AstBlock *block_body = (AstBlock *) BreakableStatementStack().Top();
  797.     do_statement -> can_complete_normally = (enclosed_statement -> can_complete_normally && ((! literal) || literal -> value == 0)) ||
  798.                                             block_body -> can_complete_normally;
  799.  
  800.     BreakableStatementStack().Pop();
  801.     ContinuableStatementStack().Pop();
  802.  
  803.     return;
  804. }
  805.  
  806.  
  807. void Semantic::ProcessBreakStatement(Ast *stmt)
  808. {
  809.     AstBreakStatement *break_statement = (AstBreakStatement *) stmt;
  810.  
  811.     //
  812.     // Recall that it is possible to break out of any labeled statement even if it is not a
  813.     // do, for, while or switch statement.
  814.     //
  815.     if (break_statement -> identifier_token_opt)
  816.     {
  817.         NameSymbol *name_symbol = lex_stream -> NameSymbol(break_statement -> identifier_token_opt);
  818.         LabelSymbol *label_symbol = LocalSymbolTable().FindLabelSymbol(name_symbol);
  819.  
  820.         if (label_symbol)
  821.         {
  822.             break_statement -> nesting_level = label_symbol -> nesting_level;
  823.             AstBlock *block_body = label_symbol -> block;
  824.             //
  825.             // A labeled statement can complete normally if there is a
  826.             // reachable break statement that exits the labeled statement.
  827.             //
  828.             if (block_body && break_statement -> is_reachable)
  829.                 block_body -> can_complete_normally = true;
  830.         }
  831.         else
  832.         {
  833.             AstBlock *block_body = (AstBlock *) LocalBlockStack().TopBlock();
  834.             break_statement -> nesting_level = block_body -> nesting_level;
  835.             ReportSemError(SemanticError::UNDECLARED_LABEL,
  836.                            break_statement -> identifier_token_opt,
  837.                            break_statement -> identifier_token_opt,
  838.                            lex_stream -> NameString(break_statement -> identifier_token_opt));
  839.         }
  840.     }
  841.     else
  842.     {
  843.         AstBlock *block_body = (AstBlock *) (BreakableStatementStack().Size() > 0 ? BreakableStatementStack().Top()
  844.                                                                                   : LocalBlockStack().TopBlock());
  845.         break_statement -> nesting_level = block_body -> nesting_level;
  846.         if (BreakableStatementStack().Size() > 0)
  847.         {
  848.             if (break_statement -> is_reachable)
  849.                 block_body -> can_complete_normally = true;
  850.         }
  851.         else ReportSemError(SemanticError::MISPLACED_BREAK_STATEMENT,
  852.                             break_statement -> LeftToken(),
  853.                             break_statement -> RightToken());
  854.     }
  855.  
  856.     return;
  857. }
  858.  
  859.  
  860. void Semantic::ProcessContinueStatement(Ast *stmt)
  861. {
  862.     AstContinueStatement *continue_statement = (AstContinueStatement *) stmt;
  863.  
  864.     //
  865.     // The loop statement that is to be continued.
  866.     //
  867.     Ast *loop_statement = NULL;
  868.  
  869.     if (ContinuableStatementStack().Size() <= 0)
  870.     {
  871.         ReportSemError(SemanticError::MISPLACED_CONTINUE_STATEMENT,
  872.                        continue_statement -> LeftToken(),
  873.                        continue_statement -> RightToken());
  874.     }
  875.     else if (continue_statement -> identifier_token_opt)
  876.     {
  877.         NameSymbol *name_symbol = lex_stream -> NameSymbol(continue_statement -> identifier_token_opt);
  878.         LabelSymbol *label_symbol = LocalSymbolTable().FindLabelSymbol(name_symbol);
  879.  
  880.         if (label_symbol)
  881.         {
  882.             continue_statement -> nesting_level = label_symbol -> nesting_level;
  883.  
  884.             assert(label_symbol -> block -> NumStatements() > 0);
  885.  
  886.             loop_statement = label_symbol -> block -> Statement(0);
  887.         }
  888.         else
  889.         {
  890.             AstBlock *block_body = (AstBlock *) LocalBlockStack().TopBlock();
  891.             continue_statement -> nesting_level = block_body -> nesting_level;
  892.             ReportSemError(SemanticError::UNDECLARED_LABEL,
  893.                            continue_statement -> identifier_token_opt,
  894.                            continue_statement -> identifier_token_opt,
  895.                            lex_stream -> NameString(continue_statement -> identifier_token_opt));
  896.         }
  897.     }
  898.     else
  899.     {
  900.         AstBlock *block_body = (AstBlock *) ContinuableStatementStack().Top();
  901.         loop_statement = block_body -> Statement(0);
  902.         continue_statement -> nesting_level = block_body -> nesting_level;
  903.     }
  904.  
  905.     //
  906.     // If this is a valid continue statement, it is associated with a loop statement.
  907.     // Since the loop can be continued, its enclosed statement "can complete normally".
  908.     //
  909.     if (loop_statement)
  910.     {
  911.         AstDoStatement *do_statement = loop_statement -> DoStatementCast();
  912.         AstForStatement *for_statement = loop_statement -> ForStatementCast();
  913.         AstWhileStatement *while_statement = loop_statement -> WhileStatementCast();
  914.  
  915.         AstStatement *enclosed_statement = (do_statement ? do_statement -> statement
  916.                                                          : (for_statement ? for_statement -> statement
  917.                                                                           : (while_statement ? while_statement -> statement
  918.                                                                                              : (AstStatement *) NULL)));
  919.         if (enclosed_statement)
  920.             enclosed_statement -> can_complete_normally = true;
  921.         else
  922.         {
  923.             assert(continue_statement -> identifier_token_opt);
  924.  
  925.             ReportSemError(SemanticError::INVALID_CONTINUE_TARGET,
  926.                            continue_statement -> LeftToken(),
  927.                            continue_statement -> RightToken(),
  928.                            lex_stream -> NameString(continue_statement -> identifier_token_opt));
  929.         }
  930.     }
  931.  
  932.     return;
  933. }
  934.  
  935.  
  936. void Semantic::ProcessReturnStatement(Ast *stmt)
  937. {
  938.     AstReturnStatement *return_statement = (AstReturnStatement *) stmt;
  939.  
  940.     MethodSymbol *this_method = ThisMethod();
  941.  
  942.     if (this_method -> name_symbol == control.clinit_name_symbol || this_method -> name_symbol == control.block_init_name_symbol)
  943.     {
  944.         ReportSemError(SemanticError::RETURN_STATEMENT_IN_INITIALIZER,
  945.                        return_statement -> LeftToken(),
  946.                        return_statement -> RightToken());
  947.     }
  948.     else if (return_statement -> expression_opt)
  949.     {
  950.         ProcessExpression(return_statement -> expression_opt);
  951.  
  952.         if (this_method -> Type() == control.void_type)
  953.         {
  954.             ReportSemError(SemanticError::MISPLACED_RETURN_WITH_EXPRESSION,
  955.                            return_statement -> LeftToken(),
  956.                            return_statement -> RightToken());
  957.         }
  958.         else if (return_statement -> expression_opt -> Type() != control.no_type)
  959.         {
  960.             if (this_method -> Type() != return_statement -> expression_opt -> Type())
  961.             {
  962.                 if (CanAssignmentConvert(this_method -> Type(), return_statement -> expression_opt))
  963.                     return_statement -> expression_opt = ConvertToType(return_statement -> expression_opt, this_method -> Type());
  964.                 else
  965.                 {
  966.                     ReportSemError(SemanticError::MISMATCHED_RETURN_AND_METHOD_TYPE,
  967.                                    return_statement -> expression_opt -> LeftToken(),
  968.                                    return_statement -> expression_opt -> RightToken(),
  969.                                    return_statement -> expression_opt -> Type() -> ContainingPackage() -> PackageName(),
  970.                                    return_statement -> expression_opt -> Type() -> ExternalName(),
  971.                                    this_method -> Type() -> ContainingPackage() -> PackageName(),
  972.                                    this_method -> Type() -> ExternalName());
  973.                 }
  974.             }
  975.         }
  976.     }
  977.     else if (this_method -> Type() != control.void_type)
  978.     {
  979.         ReportSemError(SemanticError::MISPLACED_RETURN_WITH_NO_EXPRESSION,
  980.                        return_statement -> LeftToken(),
  981.                        return_statement -> RightToken());
  982.     }
  983.  
  984.     return;
  985. }
  986.  
  987.  
  988. bool Semantic::CatchableException(TypeSymbol *exception)
  989. {
  990.     //
  991.     // An unchecked exception or an error is ok !!
  992.     //
  993.     if (! CheckedException(exception) || exception == control.no_type)
  994.         return true;
  995.  
  996.     //
  997.     // Firstly, check the stack of try statements to see if the exception in question is catchable.
  998.     //
  999.     for (int i = TryStatementStack().Size() - 1; i >= 0; i--)
  1000.     {
  1001.         AstTryStatement *try_statement = (AstTryStatement *) TryStatementStack()[i];
  1002.  
  1003.         //
  1004.         // If a try statement contains a finally clause that can complete abruptly
  1005.         // then any exception that can reach it is assumed to be catchable.
  1006.         // See Spec 11.3.
  1007.         //
  1008.         if (try_statement -> finally_clause_opt && (! try_statement -> finally_clause_opt -> block -> can_complete_normally))
  1009.             return true;
  1010.  
  1011.         //
  1012.         // Check each catch clause in turn.
  1013.         //
  1014.         for (int k = 0; k < try_statement -> NumCatchClauses(); k++)
  1015.         {
  1016.             AstCatchClause *clause = try_statement -> CatchClause(k);
  1017.             VariableSymbol *symbol = clause -> parameter_symbol;
  1018.             if (CanAssignmentConvertReference(symbol -> Type(), exception))
  1019.                 return true;
  1020.         }
  1021.     }
  1022.  
  1023.     //
  1024.     // If we are processing the initialization expression of a field,
  1025.     // ThisMethod() is not defined.
  1026.     //
  1027.     MethodSymbol *this_method = ThisMethod();
  1028.     if (this_method)
  1029.     {
  1030.         for (int l = this_method -> NumThrows() - 1; l >= 0; l--)
  1031.         {
  1032.             if (CanAssignmentConvertReference(this_method -> Throws(l), exception))
  1033.                 return true;
  1034.         }
  1035.  
  1036.         if (this_method -> NumInitializerConstructors() > 0)
  1037.         {
  1038.             int j;
  1039.             for (j = this_method -> NumInitializerConstructors() - 1; j >= 0; j--)
  1040.             {
  1041.                 MethodSymbol *method = this_method -> InitializerConstructor(j);
  1042.                 int k;
  1043.                 for (k = method -> NumThrows() - 1; k >= 0; k--)
  1044.                 {
  1045.                     if (CanAssignmentConvertReference(method -> Throws(k), exception))
  1046.                         break;
  1047.                 }
  1048.                 if (k < 0) // no hit was found in method.
  1049.                     return false;
  1050.             }
  1051.             if (j < 0) // all the relevant constructors can catch the exception
  1052.                 return true;
  1053.         }
  1054.     }
  1055.  
  1056.     return false;
  1057. }
  1058.  
  1059.  
  1060. void Semantic::ProcessThrowStatement(Ast *stmt)
  1061. {
  1062.     AstThrowStatement *throw_statement = (AstThrowStatement *) stmt;
  1063.  
  1064.     ProcessExpression(throw_statement -> expression);
  1065.     TypeSymbol *type = throw_statement -> expression -> Type();
  1066.  
  1067.     if (type != control.no_type && (! CanAssignmentConvertReference(control.Throwable(), type)))
  1068.     {
  1069.         ReportSemError(SemanticError::EXPRESSION_NOT_THROWABLE,
  1070.                        throw_statement -> LeftToken(),
  1071.                        throw_statement -> RightToken());
  1072.     }
  1073.  
  1074.     SymbolSet *exception_set = TryExceptionTableStack().Top();
  1075.     if (exception_set)
  1076.         exception_set -> AddElement(type);
  1077.  
  1078.     if (! CatchableException(type))
  1079.     {
  1080.         MethodSymbol *this_method = ThisMethod();
  1081.         MethodSymbol *method = (this_method && this_method -> Identity() != control.clinit_name_symbol
  1082.                                             && this_method -> Identity() != control.block_init_name_symbol
  1083.                                                             ? this_method
  1084.                                                             : (MethodSymbol *) NULL);
  1085.  
  1086.         if (TryStatementStack().Size() > 0)
  1087.             ReportSemError(SemanticError::BAD_THROWABLE_EXPRESSION_IN_TRY,
  1088.                            throw_statement -> LeftToken(),
  1089.                            throw_statement -> RightToken(),
  1090.                            type -> ContainingPackage() -> PackageName(),
  1091.                            type -> ExternalName(),
  1092.                            (method ? method -> Header() : StringConstant::US_EMPTY));
  1093.         else if (method)
  1094.              ReportSemError(SemanticError::BAD_THROWABLE_EXPRESSION_IN_METHOD,
  1095.                             throw_statement -> LeftToken(),
  1096.                             throw_statement -> RightToken(),
  1097.                             type -> ContainingPackage() -> PackageName(),
  1098.                             type -> ExternalName(),
  1099.                             method -> Header());
  1100.         else ReportSemError(SemanticError::BAD_THROWABLE_EXPRESSION,
  1101.                             throw_statement -> LeftToken(),
  1102.                             throw_statement -> RightToken(),
  1103.                             type -> ContainingPackage() -> PackageName(),
  1104.                             type -> ExternalName());
  1105.     }
  1106.  
  1107.     return;
  1108. }
  1109.  
  1110.  
  1111. void Semantic::ProcessTryStatement(Ast *stmt)
  1112. {
  1113.     AstTryStatement *try_statement = (AstTryStatement *) stmt;
  1114.  
  1115.     //
  1116.     // A try_statement containing a finally clause requires some extra local
  1117.     // variables in its immediately enclosing block. If it is enclosed in a method
  1118.     // that returns void then 2 extra elements are needed. If the method
  1119.     // returns a long or a double value, two additional elements are needed.
  1120.     // Otherwise, one additional element is needed.
  1121.     // If this try_statement is the first try_statement with a finally clause
  1122.     // that was encountered in the immediately enclosing block, we allocate
  1123.     // two extra slots for the special local variables.
  1124.     //
  1125.     AstBlock *enclosing_block = LocalBlockStack().TopBlock();
  1126.     if (try_statement -> finally_clause_opt)
  1127.     {
  1128.         BlockSymbol *block = enclosing_block -> block_symbol;
  1129.         if (block -> try_variable_index == 0) // first try_statement encountered in enclosing block?
  1130.         {
  1131.             block -> try_variable_index = block -> max_variable_index;
  1132.             block -> max_variable_index += 2;
  1133.             if (ThisMethod() -> Type() != control.void_type)
  1134.             {
  1135.                  if (control.IsDoubleWordType(ThisMethod() -> Type()))
  1136.                       block -> max_variable_index += 2;
  1137.                  else block -> max_variable_index += 1;
  1138.             }
  1139.         }
  1140.  
  1141.         //
  1142.         // A finally block is processed in the environment of its immediate enclosing block.
  1143.         // (as opposed to the environment of its associated try block).
  1144.         //
  1145.         // Note that the finally block must be processed prior to the other
  1146.         // blocks in the try statement, because the computation of whether or not
  1147.         // an exception is catchable in a try statement depends on the termination
  1148.         // status of the associated finally block. See CatchableException function.
  1149.         //
  1150.         AstBlock *block_body = try_statement -> finally_clause_opt -> block;
  1151.         block_body -> is_reachable = try_statement -> is_reachable;
  1152.         ProcessBlock(block_body);
  1153.     }
  1154.  
  1155.     //
  1156.     // Note that the catch clauses are processed first - prior to processing
  1157.     // the main block - so that we can have their parameters available when we
  1158.     // are processing the main block, in case that block contains a throw
  1159.     // statement. See ProcessThrowStatement for more information.
  1160.     //
  1161.     // Also, recall that the body of the catch blocks must not be
  1162.     // processed within the environment of the associated try whose
  1163.     // exceptions they are supposed to catch but within the immediate enclosing
  1164.     // block (which may itself be a try block).
  1165.     //
  1166.     for (int i = 0; i < try_statement -> NumCatchClauses(); i++)
  1167.     {
  1168.         AstCatchClause *clause = try_statement -> CatchClause(i);
  1169.         AstFormalParameter *parameter = clause -> formal_parameter;
  1170.  
  1171.         TypeSymbol *parm_type;
  1172.  
  1173.         if (parameter -> type -> PrimitiveTypeCast())
  1174.         {
  1175.             ReportSemError(SemanticError::CATCH_PRIMITIVE_TYPE,
  1176.                            parameter -> LeftToken(),
  1177.                            parameter -> RightToken());
  1178.             parm_type = control.Error();
  1179.         }
  1180.         else if (parameter -> type -> ArrayTypeCast())
  1181.         {
  1182.             ReportSemError(SemanticError::CATCH_ARRAY_TYPE,
  1183.                            parameter -> LeftToken(),
  1184.                            parameter -> RightToken());
  1185.             parm_type = control.Error();
  1186.         }
  1187.         else parm_type = MustFindType(parameter -> type);
  1188.  
  1189.         if (! parm_type -> IsSubclass(control.Throwable()))
  1190.         {
  1191.             ReportSemError(SemanticError::TYPE_NOT_THROWABLE,
  1192.                            parameter -> LeftToken(),
  1193.                            parameter -> RightToken(),
  1194.                            parm_type -> ContainingPackage() -> PackageName(),
  1195.                            parm_type -> ExternalName());
  1196.         }
  1197.  
  1198.         AstVariableDeclaratorId *name = parameter -> formal_declarator -> variable_declarator_name;
  1199.         NameSymbol *name_symbol = lex_stream -> NameSymbol(name -> identifier_token);
  1200.         if (LocalSymbolTable().FindVariableSymbol(name_symbol))
  1201.         {
  1202.             ReportSemError(SemanticError::DUPLICATE_LOCAL_VARIABLE_DECLARATION,
  1203.                            name -> identifier_token,
  1204.                            name -> identifier_token,
  1205.                            name_symbol -> Name());
  1206.         }
  1207.  
  1208.         AstBlock *block_body = clause -> block;
  1209.         //
  1210.         // Guess that the number of elements in the table will not exceed the number of statements + the clause parameter.
  1211.         //
  1212.         BlockSymbol *block = LocalSymbolTable().Top() -> InsertBlockSymbol(block_body -> NumStatements() + 1);
  1213.         block -> max_variable_index = enclosing_block -> block_symbol -> max_variable_index;
  1214.         LocalSymbolTable().Push(block -> Table());
  1215.  
  1216.         if ((! control.option.one_one) && parameter -> NumParameterModifiers() > 0)
  1217.         {
  1218.             ReportSemError(SemanticError::ONE_ONE_FEATURE,
  1219.                            parameter -> ParameterModifier(0) -> LeftToken(),
  1220.                            parameter -> ParameterModifier(0) -> RightToken());
  1221.         }
  1222.         AccessFlags access_flags = ProcessFormalModifiers(parameter);
  1223.  
  1224.         VariableSymbol *symbol = LocalSymbolTable().Top() -> InsertVariableSymbol(name_symbol);
  1225.         symbol -> SetFlags(access_flags);
  1226.         symbol -> SetType(parm_type);
  1227.         symbol -> SetOwner(ThisMethod());
  1228.         symbol -> SetLocalVariableIndex(block -> max_variable_index++);
  1229.         symbol -> declarator = parameter -> formal_declarator;
  1230.  
  1231.         clause -> parameter_symbol = symbol;
  1232.  
  1233.         //
  1234.         // Note that for the purpose of semantic checking we assume that
  1235.         // the body of the catch block is reachable. Whether or not the catch
  1236.         // statement can be executed at all is checked later.
  1237.         //
  1238.         block_body -> is_reachable = true;
  1239.  
  1240.         block_body -> block_symbol = block;
  1241.         block_body -> nesting_level = LocalBlockStack().Size();
  1242.         LocalBlockStack().Push(block_body);
  1243.  
  1244.         ProcessBlockStatements(block_body);
  1245.  
  1246.         LocalBlockStack().Pop();
  1247.         LocalSymbolTable().Pop();
  1248.  
  1249.         //
  1250.         // Update the information for the block that immediately encloses the current block.
  1251.         //
  1252.         if (LocalBlockStack().TopMaxEnclosedVariableIndex() < block -> max_variable_index)
  1253.             LocalBlockStack().TopMaxEnclosedVariableIndex() = block -> max_variable_index;
  1254.  
  1255.         //
  1256.         // If a catch clause block can complete normally, we assume
  1257.         // that the try statement can complete normally. This may
  1258.         // prove to be false later if we find out that the finally
  1259.         // clause cannot complete normally...
  1260.         //
  1261.         if (block_body -> can_complete_normally)
  1262.             try_statement -> can_complete_normally = true;
  1263.  
  1264.         block -> CompressSpace(); // space optimization
  1265.     }
  1266.  
  1267.     //
  1268.     //
  1269.     //
  1270.     TryStatementStack().Push(try_statement);
  1271.     SymbolSet *exception_set = new SymbolSet;
  1272.     TryExceptionTableStack().Push(exception_set);
  1273.  
  1274.     try_statement -> block -> is_reachable = try_statement -> is_reachable;
  1275.     ProcessBlock(try_statement -> block);
  1276.     if (try_statement -> block -> can_complete_normally)
  1277.         try_statement -> can_complete_normally = true;
  1278.  
  1279.     //
  1280.     // A catch block is reachable iff both of the following are true:
  1281.     //
  1282.     //     . Some expression or throw statement in the try block is reachable
  1283.     //       and can throw an exception that is assignable to the parameter
  1284.     //       of the catch clause C.
  1285.     //
  1286.     //     . There is no earlier catch block A in the try statement such that the
  1287.     //       type of C's parameter is the same as or a subclass of the type of A's
  1288.     //       parameter.
  1289.     //
  1290.     Tuple<TypeSymbol *> thrown_exceptions;
  1291.     for (int l = 0; l < try_statement -> NumCatchClauses(); l++)
  1292.     {
  1293.         AstCatchClause *clause = try_statement -> CatchClause(l);
  1294.         VariableSymbol *symbol = clause -> parameter_symbol;
  1295.         if (CheckedException(symbol -> Type()))
  1296.         {
  1297.             int initial_length = thrown_exceptions.Length();
  1298.  
  1299.             for (TypeSymbol *exception = (TypeSymbol *) exception_set -> FirstElement();
  1300.                  exception;
  1301.                  exception = (TypeSymbol *) exception_set -> NextElement())
  1302.             {
  1303.                 if (CanAssignmentConvertReference(symbol -> Type(), exception) ||
  1304.                     CanAssignmentConvertReference(exception, symbol -> Type()))
  1305.                     thrown_exceptions.Next() = exception;
  1306.             }
  1307.  
  1308.             if (thrown_exceptions.Length() == initial_length) // no assignable exception was found
  1309.             {
  1310.                 ReportSemError(SemanticError::UNREACHABLE_CATCH_CLAUSE,
  1311.                                clause -> formal_parameter -> LeftToken(),
  1312.                                clause -> formal_parameter -> RightToken(),
  1313.                                symbol -> Type() -> ContainingPackage() -> PackageName(),
  1314.                                symbol -> Type() -> ExternalName());
  1315.             }
  1316.             else
  1317.             {
  1318.                 //
  1319.                 // TODO: I know, I know, this is a sequential search...
  1320.                 //
  1321.                 AstCatchClause *previous_clause;
  1322.                 int k;
  1323.                 for (k = 0; k < l; k++)
  1324.                 {
  1325.                     previous_clause = try_statement -> CatchClause(k);
  1326.                     if (symbol -> Type() -> IsSubclass(previous_clause -> parameter_symbol -> Type()))
  1327.                         break;
  1328.                 }
  1329.  
  1330.                 if (k < l)
  1331.                 {
  1332.                      FileLocation loc(lex_stream, previous_clause -> formal_parameter -> RightToken());
  1333.                      ReportSemError(SemanticError::BLOCKED_CATCH_CLAUSE,
  1334.                                     clause -> formal_parameter -> LeftToken(),
  1335.                                     clause -> formal_parameter -> RightToken(),
  1336.                                     symbol -> Type() -> ContainingPackage() -> PackageName(),
  1337.                                     symbol -> Type() -> ExternalName(),
  1338.                                     loc.location);
  1339.                 }
  1340.                 else clause -> block -> is_reachable = true;
  1341.             }
  1342.         }
  1343.     }
  1344.  
  1345.     TryStatementStack().Pop();
  1346.     TryExceptionTableStack().Pop();
  1347.     if (TryExceptionTableStack().Top())
  1348.     {
  1349.         //
  1350.         // First, remove all the thrown exceptions that are caught by the enclosing
  1351.         // try statement. Then, add the remaining ones to the set that must be caught
  1352.         // by the immediately enclosing try statement.
  1353.         //
  1354.         for (int i = 0; i < thrown_exceptions.Length(); i++)
  1355.             exception_set -> RemoveElement(thrown_exceptions[i]);
  1356.         TryExceptionTableStack().Top() -> Union(*exception_set);
  1357.     }
  1358.     delete exception_set;
  1359.  
  1360.     //
  1361.     // A try statement cannot complete normally if it contains a finally
  1362.     // clause that cannot complete normally.
  1363.     //
  1364.     if (try_statement -> finally_clause_opt && (! try_statement -> finally_clause_opt -> block -> can_complete_normally))
  1365.         try_statement -> can_complete_normally = false;
  1366.  
  1367.     return;
  1368. }
  1369.  
  1370.  
  1371. void Semantic::ProcessEmptyStatement(Ast *stmt)
  1372. {
  1373.     AstEmptyStatement *empty_statement = (AstEmptyStatement *) stmt;
  1374.  
  1375.     //
  1376.     // An empty statement can complete normally iff it is reachable.
  1377.     //
  1378.     empty_statement -> can_complete_normally = empty_statement -> is_reachable;
  1379.  
  1380.     return;
  1381. }
  1382.  
  1383.  
  1384. TypeSymbol *Semantic::GetLocalType(AstClassDeclaration *class_declaration)
  1385. {
  1386.     NameSymbol *name_symbol = lex_stream -> NameSymbol(class_declaration -> identifier_token);
  1387.     TypeSymbol *type = LocalSymbolTable().Top() -> InsertNestedTypeSymbol(name_symbol);
  1388.  
  1389.     TypeSymbol *outermost_type = ThisType() -> outermost_type;
  1390.     if (! outermost_type -> local)
  1391.         outermost_type -> local = new SymbolSet;
  1392.  
  1393.     IntToWstring value(outermost_type -> local -> NameCount(name_symbol) + 1);
  1394.  
  1395.     int length = value.Length() + outermost_type -> NameLength() + 1 + name_symbol -> NameLength() + 1; // +1 for $,... +1 for $
  1396.     wchar_t *external_name = new wchar_t[length + 1]; // +1 for '\0';
  1397.     wcscpy(external_name, outermost_type -> Name());
  1398.     wcscat(external_name, StringConstant::US__DS);
  1399.     wcscat(external_name, value.String());
  1400.     wcscat(external_name, StringConstant::US__DS);
  1401.     wcscat(external_name, name_symbol -> Name());
  1402.  
  1403.     type -> SetACC_PRIVATE();
  1404.     type -> outermost_type = outermost_type;
  1405.     type -> SetExternalIdentity(control.FindOrInsertName(external_name, length));
  1406.     outermost_type -> local -> AddElement(type);
  1407.  
  1408.     delete [] external_name;
  1409.  
  1410.     return type;
  1411. }
  1412.  
  1413.  
  1414. void Semantic::ProcessClassDeclaration(Ast *stmt)
  1415. {
  1416.     AstClassDeclaration *class_declaration = (AstClassDeclaration *) stmt;
  1417.     AstClassBody *class_body = class_declaration -> class_body;
  1418.  
  1419.     class_declaration -> MarkLocal(); // identify class as "statement" and assert that it is "reachable" and "can_complete_normally"
  1420.     if (! control.option.one_one)
  1421.     {
  1422.         ReportSemError(SemanticError::ONE_ONE_FEATURE,
  1423.                        class_declaration -> LeftToken(),
  1424.                        class_declaration -> RightToken());
  1425.     }
  1426.  
  1427.     CheckNestedTypeDuplication(state_stack.Top(), class_declaration -> identifier_token);
  1428.  
  1429.     NameSymbol *name_symbol = lex_stream -> NameSymbol(class_declaration -> identifier_token);
  1430.  
  1431.     TypeSymbol *inner_type = GetLocalType(class_declaration);
  1432.     inner_type -> outermost_type = ThisType() -> outermost_type;
  1433.     inner_type -> supertypes_closure = new SymbolSet;
  1434.     inner_type -> subtypes_closure = new SymbolSet;
  1435.     inner_type -> subtypes = new SymbolSet;
  1436.     inner_type -> semantic_environment = new SemanticEnvironment((Semantic *) this,
  1437.                                                                  inner_type,
  1438.                                                                  state_stack.Top());
  1439.     inner_type -> declaration = class_declaration;
  1440.     inner_type -> file_symbol = source_file_symbol;
  1441.     inner_type -> SetFlags(ProcessLocalClassModifiers(class_declaration));
  1442.     inner_type -> SetOwner(ThisMethod());
  1443.     //
  1444.     // Add 3 extra elements for padding. May need a default constructor and other support elements.
  1445.     //
  1446.     inner_type -> SetSymbolTable(class_body -> NumClassBodyDeclarations() + 3);
  1447.     inner_type -> SetLocation();
  1448.     inner_type -> SetSignature(control);
  1449.  
  1450.     if (StaticRegion())
  1451.          inner_type -> SetACC_STATIC();
  1452.     else inner_type -> InsertThis(0);
  1453.  
  1454.     class_declaration -> semantic_environment = inner_type -> semantic_environment; // save for processing bodies later.
  1455.  
  1456.     CheckClassMembers(inner_type, class_body);
  1457.  
  1458.     ProcessTypeHeaders(class_declaration);
  1459.  
  1460.     ProcessMembers(class_declaration -> semantic_environment, class_body);
  1461.     CompleteSymbolTable(class_declaration -> semantic_environment, class_declaration -> identifier_token, class_body);
  1462.     ProcessExecutableBodies(class_declaration -> semantic_environment, class_body);
  1463.  
  1464.     UpdateLocalConstructors(inner_type);
  1465.  
  1466.     return;
  1467. }
  1468.  
  1469.  
  1470. void Semantic::ProcessThisCall(AstThisCall *this_call)
  1471. {
  1472.     TypeSymbol *this_type = ThisType(),
  1473.                *containing_type = this_type -> ContainingType();
  1474.  
  1475.     ExplicitConstructorInvocation() = this_call; // signal that we are about to process an explicit constructor invocation
  1476.  
  1477.     if (this_call -> base_opt)
  1478.     {
  1479.         if (! control.option.one_one)
  1480.         {
  1481.             ReportSemError(SemanticError::ONE_ONE_FEATURE,
  1482.                            this_call -> base_opt -> LeftToken(),
  1483.                            this_call -> dot_token_opt);
  1484.         }
  1485.  
  1486.         ProcessExpression(this_call -> base_opt);
  1487.  
  1488.         TypeSymbol *expr_type = this_call -> base_opt -> Type();
  1489.         if (expr_type != control.no_type)
  1490.         {
  1491.             if (! containing_type)
  1492.             {
  1493.                 ReportSemError(SemanticError::TYPE_NOT_INNER_CLASS,
  1494.                                this_call -> base_opt -> LeftToken(),
  1495.                                this_call -> base_opt -> RightToken(),
  1496.                                this_type -> ContainingPackage() -> PackageName(),
  1497.                                this_type -> ExternalName(),
  1498.                                expr_type -> ContainingPackage() -> PackageName(),
  1499.                                expr_type -> ExternalName());
  1500.                 this_call -> base_opt -> symbol = control.no_type;
  1501.             }
  1502.             //
  1503.             // 1.2 change. In 1.1, we used to allow access to any subclass of type. Now, there must
  1504.             // be a perfect match.
  1505.             //
  1506.             // else if (! expr_type -> IsSubclass(containing_type))
  1507.             //
  1508.             else if (expr_type != containing_type)
  1509.             {
  1510.                 ReportSemError(SemanticError::INVALID_ENCLOSING_INSTANCE,
  1511.                                this_call -> base_opt -> LeftToken(),
  1512.                                this_call -> base_opt -> RightToken(),
  1513.                                this_type -> ContainingPackage() -> PackageName(),
  1514.                                this_type -> ExternalName(),
  1515.                                containing_type -> ContainingPackage() -> PackageName(),
  1516.                                containing_type -> ExternalName(),
  1517.                                expr_type -> ContainingPackage() -> PackageName(),
  1518.                                expr_type -> ExternalName());
  1519.                 this_call -> base_opt -> symbol = control.no_type;
  1520.             }
  1521.         }
  1522.     }
  1523.     else // (! this_call -> base_opt)
  1524.     {
  1525.         if (this_type -> IsInner())
  1526.             this_call -> base_opt = CreateAccessToType(this_call, containing_type);
  1527.     }
  1528.  
  1529.     bool no_bad_argument = true;
  1530.  
  1531.     for (int i = 0; i < this_call -> NumArguments(); i++)
  1532.     {
  1533.         AstExpression *expr = (AstExpression *) this_call -> Argument(i);
  1534.         ProcessExpressionOrStringConstant(expr);
  1535.         no_bad_argument = no_bad_argument && (expr -> Type() != control.no_type);
  1536.     }
  1537.     if (no_bad_argument)
  1538.     {
  1539.         MethodSymbol *constructor = FindConstructor(this_type, this_call, this_call -> this_token, this_call -> RightToken());
  1540.         if (constructor)
  1541.         {
  1542.             this_call -> symbol = constructor;
  1543.  
  1544.             for (int i = 0; i < this_call -> NumArguments(); i++)
  1545.             {
  1546.                 AstExpression *expr = this_call -> Argument(i);
  1547.                 if (expr -> Type() != constructor -> FormalParameter(i) -> Type())
  1548.                     this_call -> Argument(i) = ConvertToType(expr, constructor -> FormalParameter(i) -> Type());
  1549.             }
  1550.  
  1551.             for (int k = constructor -> NumThrows() - 1; k >= 0; k--)
  1552.             {
  1553.                 TypeSymbol *exception = constructor -> Throws(k);
  1554.                 if (! CatchableException(exception))
  1555.                 {
  1556.                     ReportSemError(SemanticError::CONSTRUCTOR_DOES_NOT_THROW_THIS_EXCEPTION,
  1557.                                    this_call -> this_token,
  1558.                                    this_call -> this_token,
  1559.                                    exception -> ContainingPackage() -> PackageName(),
  1560.                                    exception -> ExternalName());
  1561.                 }
  1562.             }
  1563.  
  1564.             if (this_type -> IsLocal()) // a local type may use enclosed local variables?
  1565.                 this_type -> AddLocalConstructorCallEnvironment(GetEnvironment(this_call));
  1566.  
  1567.             //
  1568.             // Note that there is no need to do access-checking as we are allowed,
  1569.             // within the body of a class, to invoke any other constructor or member
  1570.             // (private or otherwise) in that class.
  1571.             //
  1572.         }
  1573.     }
  1574.  
  1575.     ExplicitConstructorInvocation() = NULL; // signal that we are no longer processing an explicit constructor invocation
  1576.  
  1577.     this_call -> can_complete_normally = this_call -> is_reachable;
  1578.  
  1579.     return;
  1580. }
  1581.  
  1582.  
  1583. void Semantic::ProcessSuperCall(AstSuperCall *super_call)
  1584. {
  1585.     TypeSymbol *this_type = ThisType();
  1586.     ExplicitConstructorInvocation() = super_call; // signal that we are about to process an explicit constructor invocation
  1587.  
  1588.     TypeSymbol *super_type = this_type -> super;
  1589.     if (! super_type) // this is only possible if we are compiling an illegal Object.java source file.
  1590.         super_type = control.Object();
  1591.  
  1592.     if (super_call -> base_opt)
  1593.     {
  1594.         if (! control.option.one_one)
  1595.         {
  1596.             ReportSemError(SemanticError::ONE_ONE_FEATURE,
  1597.                            super_call -> base_opt -> LeftToken(),
  1598.                            super_call -> dot_token_opt);
  1599.         }
  1600.  
  1601.         ProcessExpression(super_call -> base_opt);
  1602.  
  1603.         TypeSymbol *expr_type = super_call -> base_opt -> Type();
  1604.         if (expr_type != control.no_type)
  1605.         {
  1606.             TypeSymbol *containing_type = super_type -> ContainingType();
  1607.             if (! containing_type)
  1608.             {
  1609.                 ReportSemError(SemanticError::SUPER_TYPE_NOT_INNER_CLASS,
  1610.                                super_call -> base_opt -> LeftToken(),
  1611.                                super_call -> base_opt -> RightToken(),
  1612.                                super_type -> ContainingPackage() -> PackageName(),
  1613.                                super_type -> ExternalName(),
  1614.                                this_type -> ContainingPackage() -> PackageName(),
  1615.                                this_type -> ExternalName(),
  1616.                                expr_type -> ContainingPackage() -> PackageName(),
  1617.                                expr_type -> ExternalName());
  1618.                 super_call -> base_opt -> symbol = control.no_type;
  1619.             }
  1620.             //
  1621.             // 1.2 change. In 1.1, we used to allow access to any subclass of type. Now, there must
  1622.             // be a perfect match.
  1623.             //
  1624.             // else if (! expr_type -> IsSubclass(containing_type))
  1625.             //
  1626.             else if (expr_type != containing_type)
  1627.             {
  1628.                 ReportSemError(SemanticError::INVALID_ENCLOSING_INSTANCE,
  1629.                                super_call -> base_opt -> LeftToken(),
  1630.                                super_call -> base_opt -> RightToken(),
  1631.                                this_type -> ContainingPackage() -> PackageName(),
  1632.                                this_type -> ExternalName(),
  1633.                                containing_type -> ContainingPackage() -> PackageName(),
  1634.                                containing_type -> ExternalName(),
  1635.                                expr_type -> ContainingPackage() -> PackageName(),
  1636.                                expr_type -> ExternalName());
  1637.                 super_call -> base_opt -> symbol = control.no_type;
  1638.             }
  1639.         }
  1640.     }
  1641.     else // (! super_call -> base_opt)
  1642.     {
  1643.         if (super_type && super_type -> IsInner())
  1644.             super_call -> base_opt = CreateAccessToType(super_call, super_type -> ContainingType());
  1645.     }
  1646.  
  1647.     bool no_bad_argument = true;
  1648.     for (int i = 0; i < super_call -> NumArguments(); i++)
  1649.     {
  1650.         AstExpression *expr = (AstExpression *) super_call -> Argument(i);
  1651.         ProcessExpressionOrStringConstant(expr);
  1652.         no_bad_argument = no_bad_argument && (expr -> Type() != control.no_type);
  1653.     }
  1654.  
  1655.     if (this_type == control.Object())
  1656.     {
  1657.         super_call -> symbol = NULL;
  1658.         ReportSemError(SemanticError::MISPLACED_SUPER_EXPRESSION,
  1659.                        super_call -> super_token,
  1660.                        super_call -> super_token);
  1661.     }
  1662.     else if (no_bad_argument)
  1663.     {
  1664.         MethodSymbol *constructor = FindConstructor(super_type, super_call, super_call -> super_token, super_call -> RightToken());
  1665.  
  1666.         if (constructor)
  1667.         {
  1668.             //
  1669.             // No need to do a full access-check. Do the minimal stuff here !
  1670.             //
  1671.             if (constructor -> ACC_PRIVATE())
  1672.             {
  1673.                 if (this_type -> outermost_type == super_type -> outermost_type)
  1674.                 {
  1675.                      if (! constructor -> LocalConstructor())
  1676.                      {
  1677.                          constructor = super_type -> GetReadAccessMethod(constructor);
  1678.  
  1679.                          //
  1680.                          // Add extra argument for read access constructor;
  1681.                          //
  1682.                          super_call -> AddNullArgument();
  1683.                      }
  1684.                 }
  1685.                 else ReportSemError(SemanticError::PRIVATE_CONSTRUCTOR_NOT_ACCESSIBLE,
  1686.                                     super_call -> LeftToken(),
  1687.                                     super_call -> RightToken(),
  1688.                                     constructor -> Header(),
  1689.                                     super_type -> ContainingPackage() -> PackageName(),
  1690.                                     super_type -> ExternalName());
  1691.             }
  1692.             else if (! (constructor -> ACC_PUBLIC() || constructor -> ACC_PROTECTED()))
  1693.             {
  1694.                 if (! (this_type -> outermost_type == super_type -> outermost_type ||
  1695.                        super_type -> ContainingPackage() == this_package))
  1696.                     ReportSemError(SemanticError::DEFAULT_CONSTRUCTOR_NOT_ACCESSIBLE,
  1697.                                    super_call -> super_token,
  1698.                                    super_call -> super_token,
  1699.                                    constructor -> Header(),
  1700.                                    super_type -> ContainingPackage() -> PackageName(),
  1701.                                    super_type -> ExternalName());
  1702.             }
  1703.  
  1704.             super_call -> symbol = constructor;
  1705.  
  1706.             if (super_call -> base_opt &&
  1707.                 (super_call -> base_opt -> Type() != control.no_type) &&
  1708.                 (super_call -> base_opt -> Type() != super_type -> ContainingType()))
  1709.             {
  1710.                 assert(CanMethodInvocationConvert(super_type -> ContainingType(), super_call -> base_opt -> Type()));
  1711.  
  1712.                 super_call -> base_opt = ConvertToType(super_call -> base_opt, super_type -> ContainingType());
  1713.             }
  1714.  
  1715.             for (int i = 0; i < super_call -> NumArguments(); i++)
  1716.             {
  1717.                 AstExpression *expr = super_call -> Argument(i);
  1718.                 if (expr -> Type() != constructor -> FormalParameter(i) -> Type())
  1719.                     super_call -> Argument(i) = ConvertToType(expr, constructor -> FormalParameter(i) -> Type());
  1720.             }
  1721.  
  1722.             //
  1723.             // Make sure that the throws signature of the constructor is processed.
  1724.             //
  1725.             for (int k = constructor -> NumThrows() - 1; k >= 0; k--)
  1726.             {
  1727.                 TypeSymbol *exception = constructor -> Throws(k);
  1728.                 if (! CatchableException(exception))
  1729.                 {
  1730.                     ReportSemError(SemanticError::CONSTRUCTOR_DOES_NOT_THROW_SUPER_EXCEPTION,
  1731.                                    super_call -> LeftToken(),
  1732.                                    super_call -> RightToken(),
  1733.                                    this_type -> Name(),
  1734.                                    exception -> ContainingPackage() -> PackageName(),
  1735.                                    exception -> ExternalName(),
  1736.                                    constructor -> containing_type -> ContainingPackage() -> PackageName(),
  1737.                                    constructor -> containing_type -> ExternalName());
  1738.                 }
  1739.             }
  1740.  
  1741.             if (super_type -> IsLocal()) // a local type may use enclosed local variables?
  1742.             {
  1743.                 if (super_type -> LocalClassProcessingCompleted())
  1744.                 {
  1745.                     assert(ThisMethod() -> LocalConstructor() && (! ThisMethod() -> IsGeneratedLocalConstructor()));
  1746.  
  1747.                     assert(constructor -> LocalConstructor() && (! constructor -> IsGeneratedLocalConstructor()));
  1748.  
  1749.                     super_call -> symbol = constructor -> LocalConstructor();
  1750.  
  1751.                     //
  1752.                     // Recall that as a side-effect, when a local shadow is created in a type
  1753.                     // an argument that will be used to initialize the local shadow that has
  1754.                     // the same identity must be passed to every constructor in the type. Since
  1755.                     // we are currently processing a constructor, such an argument must be available.
  1756.                     //
  1757.                     BlockSymbol *block_symbol = ThisMethod() -> LocalConstructor() -> block_symbol;
  1758.                     for (int i = (super_type -> ACC_STATIC() ? 0 : 1); i < super_type -> NumConstructorParameters(); i++)
  1759.                     {
  1760.                         VariableSymbol *local = super_type -> ConstructorParameter(i) -> accessed_local,
  1761.                                        *local_shadow = this_type -> FindOrInsertLocalShadow(local);
  1762.  
  1763.                         AstSimpleName *simple_name = compilation_unit -> ast_pool -> GenSimpleName(super_call -> super_token);
  1764.                         simple_name -> symbol = block_symbol -> FindVariableSymbol(local_shadow -> Identity());
  1765.  
  1766.                         assert(simple_name -> symbol);
  1767.  
  1768.                         super_call -> AddLocalArgument(simple_name);
  1769.                     }
  1770.                 }
  1771.                 else // are we currently within the body of the type in question ?
  1772.                 {
  1773.                     super_type -> AddLocalConstructorCallEnvironment(GetEnvironment(super_call));
  1774.                 }
  1775.             }
  1776.         }
  1777.     }
  1778.  
  1779.     ExplicitConstructorInvocation() = NULL; // signal that we are no longer processing an explicit constructor invocation
  1780.  
  1781.     super_call -> can_complete_normally = super_call -> is_reachable;
  1782.  
  1783.     return;
  1784. }
  1785.  
  1786.  
  1787. void Semantic::CheckThrow(AstExpression *throw_expression)
  1788. {
  1789.     TypeSymbol *throw_type = throw_expression -> symbol -> TypeCast();
  1790.  
  1791.     assert(throw_type);
  1792.  
  1793.     if (throw_type -> ACC_INTERFACE())
  1794.     {
  1795.         ReportSemError(SemanticError::NOT_A_CLASS,
  1796.                        throw_expression -> LeftToken(),
  1797.                        throw_expression -> RightToken(),
  1798.                        throw_type -> ContainingPackage() -> PackageName(),
  1799.                        throw_type -> ExternalName());
  1800.     }
  1801.     else if (! throw_type -> IsSubclass(control.Throwable()))
  1802.     {
  1803.         ReportSemError(SemanticError::TYPE_NOT_THROWABLE,
  1804.                        throw_expression -> LeftToken(),
  1805.                        throw_expression -> RightToken(),
  1806.                        throw_type -> ContainingPackage() -> PackageName(),
  1807.                        throw_type -> ExternalName());
  1808.     }
  1809.  
  1810.     return;
  1811. }
  1812.  
  1813.  
  1814. void Semantic::ProcessMethodBody(AstMethodDeclaration *method_declaration)
  1815. {
  1816.     MethodSymbol *this_method = ThisMethod();
  1817.  
  1818.     for (int k = 0; k < method_declaration -> NumThrows(); k++)
  1819.         CheckThrow(method_declaration -> Throw(k));
  1820.  
  1821.     AstMethodDeclarator *method_declarator = method_declaration -> method_declarator;
  1822.  
  1823.     if (! method_declaration -> method_body -> EmptyStatementCast())
  1824.     {
  1825.         //
  1826.         // The block that is the body of a method is reachable
  1827.         //
  1828.         AstBlock *block_body;
  1829.  
  1830.         //
  1831.         // The body of a method must be a regular block. If instead, it
  1832.         // is a constructor block, mark the compilation unit as a bad
  1833.         // compilation so that the parser can properly diagnose this
  1834.         // problem later.
  1835.         //
  1836.         AstConstructorBlock *constructor_block = method_declaration -> method_body -> ConstructorBlockCast();
  1837.         if (constructor_block)
  1838.         {
  1839.             compilation_unit -> kind = Ast::BAD_COMPILATION; // invalidate the compilation unit
  1840.  
  1841.             constructor_block -> is_reachable = true;
  1842.             block_body = constructor_block -> block;
  1843.  
  1844.             //
  1845.             // If the parser recognizes the body of a method as a ConstructorBlock
  1846.             // then it must have an explicit_constructor_invocation.
  1847.             //
  1848.             AstThisCall *this_call = constructor_block -> explicit_constructor_invocation_opt -> ThisCallCast();
  1849.             if (this_call)
  1850.             {
  1851.                 this_call -> is_reachable = true;
  1852.                 //
  1853.                 // Do not process the explicit constructor invocation as this could
  1854.                 // cause problems with assertions (e.g. for inner classes) that will
  1855.                 // turn out not to be true.
  1856.                 //
  1857.                 // ProcessThisCall(this_call);
  1858.                 //
  1859.                 block_body -> is_reachable = this_call -> can_complete_normally;
  1860.             }
  1861.             else
  1862.             {
  1863.                 AstSuperCall *super_call = (AstSuperCall *) constructor_block -> explicit_constructor_invocation_opt;
  1864.                 super_call -> is_reachable = true;
  1865.                 //
  1866.                 // Do not process the explicit constructor invocation as this could
  1867.                 // cause problems with assertions (e.g. for inner classes) that will
  1868.                 // turn out not to be true.
  1869.                 //
  1870.                 // ProcessSuperCall(super_call);
  1871.                 //
  1872.                 block_body -> is_reachable = super_call -> can_complete_normally;
  1873.             }
  1874.         }
  1875.         else
  1876.         {
  1877.             block_body = (AstBlock *) method_declaration -> method_body;
  1878.             block_body -> is_reachable = true;
  1879.         }
  1880.  
  1881.         block_body -> block_symbol = this_method -> block_symbol;
  1882.         block_body -> nesting_level = LocalBlockStack().Size();
  1883.         LocalBlockStack().Push(block_body);
  1884.  
  1885.         ProcessBlockStatements(block_body);
  1886.  
  1887.         LocalBlockStack().Pop();
  1888.  
  1889.         if (block_body -> can_complete_normally)
  1890.         {
  1891.             if (this_method -> Type() == control.void_type)
  1892.             {
  1893.                 AstReturnStatement *return_statement = compilation_unit -> ast_pool -> GenReturnStatement();
  1894.                 return_statement -> return_token = block_body -> right_brace_token;
  1895.                 return_statement -> expression_opt = NULL;
  1896.                 return_statement -> semicolon_token = block_body -> right_brace_token;
  1897.                 return_statement -> is_reachable = true;
  1898.                 block_body -> can_complete_normally = false;
  1899.                 block_body -> AddStatement(return_statement);
  1900.             }
  1901.             else
  1902.             {
  1903.                 ReportSemError(SemanticError::TYPED_METHOD_WITH_NO_RETURN,
  1904.                                method_declaration -> type -> LeftToken(),
  1905.                                method_declaration -> method_declarator -> identifier_token,
  1906.                                this_method -> Header(),
  1907.                                this_method -> Type() -> Name());
  1908.             }
  1909.         }
  1910.  
  1911.         if (this_method -> ACC_ABSTRACT() || this_method -> ACC_NATIVE())
  1912.         {
  1913.             ReportSemError(SemanticError::ABSTRACT_METHOD_WITH_BODY,
  1914.                            method_declaration -> LeftToken(),
  1915.                            method_declaration -> RightToken(),
  1916.                            this_method -> Header());
  1917.         }
  1918.     }
  1919.     else if (! (this_method -> ACC_ABSTRACT() || this_method -> ACC_NATIVE()))
  1920.     {
  1921.         ReportSemError(SemanticError::NON_ABSTRACT_METHOD_WITHOUT_BODY,
  1922.                        method_declaration -> LeftToken(),
  1923.                        method_declaration -> RightToken(),
  1924.                        this_method -> Header());
  1925.     }
  1926.  
  1927.     this_method -> block_symbol -> CompressSpace(); // space optimization
  1928.  
  1929.     return;
  1930. }
  1931.  
  1932.  
  1933. void Semantic::ProcessConstructorBody(AstConstructorDeclaration *constructor_declaration, bool body_reachable)
  1934. {
  1935.     TypeSymbol *this_type = ThisType();
  1936.     MethodSymbol *this_method = ThisMethod();
  1937.  
  1938.     for (int k = 0; k < constructor_declaration -> NumThrows(); k++)
  1939.         CheckThrow(constructor_declaration -> Throw(k));
  1940.  
  1941.     AstMethodDeclarator *constructor_declarator = constructor_declaration -> constructor_declarator;
  1942.  
  1943.     //
  1944.     // The block that is the body of a constructor is reachable
  1945.     //
  1946.     AstConstructorBlock *constructor_block = constructor_declaration -> constructor_body;
  1947.  
  1948.     constructor_block -> is_reachable = true;
  1949.     AstBlock *block_body = constructor_block -> block;
  1950.     AstThisCall *this_call = NULL;
  1951.     AstSuperCall *super_call = NULL;
  1952.     TypeSymbol *super_type = this_type -> super;
  1953.  
  1954.     if (constructor_block -> explicit_constructor_invocation_opt)
  1955.     {
  1956.         this_call = constructor_block -> explicit_constructor_invocation_opt -> ThisCallCast();
  1957.         super_call = constructor_block -> explicit_constructor_invocation_opt -> SuperCallCast();
  1958.     }
  1959.     else if (super_type)
  1960.     {
  1961.         LexStream::TokenIndex loc = block_body -> LeftToken();
  1962.         super_call                            = compilation_unit -> ast_pool -> GenSuperCall();
  1963.         super_call -> base_opt                = NULL;
  1964.         super_call -> dot_token_opt           = loc;
  1965.         super_call -> super_token             = loc;
  1966.         super_call -> left_parenthesis_token  = loc;
  1967.         super_call -> right_parenthesis_token = loc;
  1968.         super_call -> semicolon_token         = loc;
  1969.  
  1970.         constructor_block -> explicit_constructor_invocation_opt = super_call;
  1971.  
  1972.         if (super_type -> IsInner() && (! this_type -> CanAccess(super_type -> ContainingType())))
  1973.         {
  1974.             ReportSemError(SemanticError::ENCLOSING_INSTANCE_NOT_ACCESSIBLE,
  1975.                            constructor_declaration -> constructor_declarator -> LeftToken(),
  1976.                            constructor_declaration -> constructor_declarator -> RightToken(),
  1977.                            super_type -> ContainingType() -> ContainingPackage() -> PackageName(),
  1978.                            super_type -> ContainingType() -> ExternalName());
  1979.         }
  1980.     }
  1981.  
  1982.     //
  1983.     // If the constructor starts with an explicit_constructor_invocation, either
  1984.     // one specified by the user or generated, we process it and set up the proper
  1985.     // local environment, if appropriate...
  1986.     //
  1987.     if (constructor_block -> explicit_constructor_invocation_opt)
  1988.     {
  1989.         //
  1990.         // If we are processing a local constructor, set up the generated environment...
  1991.         //
  1992.         if (this_type -> IsLocal())
  1993.         {
  1994.             LocalSymbolTable().Pop();
  1995.  
  1996.             assert(this_method -> LocalConstructor() && (! this_method -> IsGeneratedLocalConstructor()));
  1997.  
  1998.             LocalSymbolTable().Push(this_method -> LocalConstructor() -> block_symbol -> Table());
  1999.         }
  2000.  
  2001.             if (this_call)
  2002.             {
  2003.                 this_call -> is_reachable = true;
  2004.                 ProcessThisCall(this_call);
  2005.             }
  2006.             else
  2007.             {
  2008.                 assert(super_call);
  2009.  
  2010.                 super_call -> is_reachable = true;
  2011.                 ProcessSuperCall(super_call);
  2012.             }
  2013.  
  2014.         //
  2015.         // If we are processing a local constructor, restore its original environment...
  2016.         //
  2017.         if (this_type -> IsLocal())
  2018.         {
  2019.             LocalSymbolTable().Pop();
  2020.             LocalSymbolTable().Push(this_method -> block_symbol -> Table());
  2021.         }
  2022.     }
  2023.  
  2024.     if (! (body_reachable || (constructor_block -> explicit_constructor_invocation_opt &&
  2025.                               constructor_block -> explicit_constructor_invocation_opt -> ThisCallCast())))
  2026.     {
  2027.         ReportSemError(SemanticError::UNREACHABLE_CONSTRUCTOR_BODY,
  2028.                        constructor_declaration -> LeftToken(),
  2029.                        constructor_declaration -> RightToken());
  2030.     }
  2031.  
  2032.     //
  2033.     // Guess that the number of elements will not exceed the number of statements.
  2034.     //
  2035.     int table_size = block_body -> NumStatements();
  2036.     BlockSymbol *block = LocalSymbolTable().Top() -> InsertBlockSymbol(table_size);
  2037.     //
  2038.     // enclosing_block is not present only when we are processing the block of a static initializer
  2039.     //
  2040.     block -> max_variable_index = this_method -> block_symbol -> max_variable_index;
  2041.     LocalSymbolTable().Push(block -> Table());
  2042.  
  2043.     block_body -> is_reachable = true;
  2044.     block_body -> block_symbol = block;
  2045.     block_body -> nesting_level = LocalBlockStack().Size();
  2046.     LocalBlockStack().Push(block_body);
  2047.  
  2048.     ProcessBlockStatements(block_body);
  2049.  
  2050.     if (block_body -> can_complete_normally)
  2051.     {
  2052.         AstReturnStatement *return_statement = compilation_unit -> ast_pool -> GenReturnStatement();
  2053.         return_statement -> return_token = block_body -> right_brace_token;
  2054.         return_statement -> expression_opt = NULL;
  2055.         return_statement -> semicolon_token = block_body -> right_brace_token;
  2056.         return_statement -> is_reachable = true;
  2057.         block_body -> can_complete_normally = false;
  2058.         block_body -> AddStatement(return_statement);
  2059.     }
  2060.  
  2061.     constructor_block -> can_complete_normally = block_body -> can_complete_normally;
  2062.  
  2063.     LocalBlockStack().Pop();
  2064.     LocalSymbolTable().Pop();
  2065.  
  2066.     //
  2067.     // Update the local variable info for the main block associated with this constructor.
  2068.     //
  2069.     if (this_method -> block_symbol -> max_variable_index < block -> max_variable_index)
  2070.         this_method -> block_symbol -> max_variable_index = block -> max_variable_index;
  2071.  
  2072.     block -> CompressSpace(); // space optimization
  2073.  
  2074.     return;
  2075. }
  2076.  
  2077.  
  2078. void Semantic::ProcessExecutableBodies(SemanticEnvironment *environment, AstClassBody *class_body)
  2079. {
  2080.     state_stack.Push(environment);
  2081.     TypeSymbol *this_type = ThisType();
  2082.  
  2083.     assert(this_type -> HeaderProcessed());
  2084.     assert(this_type -> ConstructorMembersProcessed());
  2085.     assert(this_type -> MethodMembersProcessed());
  2086.     assert(this_type -> FieldMembersProcessed());
  2087.  
  2088.     ThisVariable() = NULL; // All variable declarations have already been processed
  2089.  
  2090.     //
  2091.     // Compute the set of instance final variables declared by the user in this type
  2092.     // as well as the set of instance final variables that have not yet been initialized.
  2093.     //
  2094.     Tuple<VariableSymbol *> finals(this_type -> NumVariableSymbols()),
  2095.                             unassigned_finals(this_type -> NumVariableSymbols());
  2096.     for (int k = 0; k < this_type -> NumVariableSymbols(); k++)
  2097.     {
  2098.         VariableSymbol *variable_symbol = this_type -> VariableSym(k);
  2099.         if (variable_symbol -> ACC_FINAL() && variable_symbol -> declarator)
  2100.         {
  2101.             finals.Next() = variable_symbol;
  2102.  
  2103.             if (! variable_symbol -> IsDefinitelyAssigned())
  2104.                 unassigned_finals.Next() = variable_symbol;
  2105.         }
  2106.     }
  2107.  
  2108.     AstBlock *last_block_body = (class_body -> NumBlocks() > 0 ? class_body -> Block(class_body -> NumBlocks() - 1)
  2109.                                                                : (AstBlock *) NULL);
  2110.     if (class_body -> NumConstructors() == 0)
  2111.     {
  2112.         //
  2113.         // Issue an error for each unassigned final.
  2114.         //
  2115.         for (int k = 0; k < unassigned_finals.Length(); k++)
  2116.         {
  2117.             ReportSemError(SemanticError::UNINITIALIZED_FINAL_VARIABLE,
  2118.                            unassigned_finals[k] -> declarator -> LeftToken(),
  2119.                            unassigned_finals[k] -> declarator -> RightToken());
  2120.         }
  2121.  
  2122.         //
  2123.         // Process the body of the default constructor, if there is one.
  2124.         // (An anonymous class does not yet have a default constructor at this point.)
  2125.         //
  2126.         AstConstructorDeclaration *constructor_decl = class_body -> default_constructor;
  2127.         if (constructor_decl)
  2128.         {
  2129.             ThisMethod() = constructor_decl -> constructor_symbol;
  2130.  
  2131.             LocalSymbolTable().Push(ThisMethod() -> block_symbol -> Table());
  2132.             LocalBlockStack().max_size = 0;
  2133.             ProcessConstructorBody(constructor_decl, ((! last_block_body) || last_block_body -> can_complete_normally));
  2134.             LocalSymbolTable().Pop();
  2135.             ThisMethod() -> max_block_depth = LocalBlockStack().max_size;
  2136.         }
  2137.     }
  2138.     else
  2139.     {
  2140.         for (int i = 0; i < class_body -> NumConstructors(); i++)
  2141.         {
  2142.             AstConstructorDeclaration *constructor_decl = class_body -> Constructor(i);
  2143.  
  2144.             ThisMethod() = constructor_decl -> constructor_symbol;
  2145.             MethodSymbol *this_method = ThisMethod();
  2146.             if (this_method)
  2147.             {
  2148.                 for (int l = 0; l < this_method -> NumFormalParameters(); l++)
  2149.                 {
  2150.                     VariableSymbol *parm = this_method -> FormalParameter(l);
  2151.                     AstVariableDeclaratorId *name = parm -> declarator -> variable_declarator_name;
  2152.  
  2153.                     SemanticEnvironment *where_found;
  2154.                     Tuple<VariableSymbol *> variables_found(2);
  2155.                     SearchForVariableInEnvironment(variables_found, where_found, state_stack.Top(),
  2156.                                                    parm -> Identity(),
  2157.                                                    name -> identifier_token);
  2158.                     VariableSymbol *symbol = (variables_found.Length() > 0 ? variables_found[0] : (VariableSymbol *) NULL);
  2159.                     if (symbol && symbol -> IsLocal())
  2160.                     {
  2161.                         ReportSemError(SemanticError::DUPLICATE_LOCAL_VARIABLE_DECLARATION,
  2162.                                        name -> identifier_token,
  2163.                                        name -> identifier_token,
  2164.                                        parm -> Name());
  2165.                     }
  2166.                 }
  2167.  
  2168.                 AstConstructorBlock *constructor_block = constructor_decl -> constructor_body;
  2169.                 if (constructor_block -> explicit_constructor_invocation_opt &&
  2170.                     constructor_block -> explicit_constructor_invocation_opt -> ThisCallCast())
  2171.                 {
  2172.                     for (int j = 0; j < unassigned_finals.Length(); j++)
  2173.                         unassigned_finals[j] -> MarkDefinitelyAssigned();
  2174.                 }
  2175.                 else
  2176.                 {
  2177.                     for (int j = 0; j < unassigned_finals.Length(); j++)
  2178.                         unassigned_finals[j] -> MarkNotDefinitelyAssigned();
  2179.                 }
  2180.  
  2181.                 LocalSymbolTable().Push(this_method -> block_symbol -> Table());
  2182.                 LocalBlockStack().max_size = 0;
  2183.  
  2184.                 int start_num_errors = NumErrors();
  2185.                 ProcessConstructorBody(constructor_decl, ((! last_block_body) || last_block_body -> can_complete_normally));
  2186.  
  2187.                 LocalSymbolTable().Pop();
  2188.                 this_method -> max_block_depth = LocalBlockStack().max_size;
  2189.  
  2190.                 if (NumErrors() == start_num_errors)
  2191.                     DefiniteConstructorBody(constructor_decl, finals);
  2192.  
  2193.                 for (int k = 0; k < unassigned_finals.Length(); k++)
  2194.                 {
  2195.                     VariableSymbol *variable_symbol = unassigned_finals[k];
  2196.                     if (! variable_symbol -> IsDefinitelyAssigned())
  2197.                     {
  2198.                         ReportSemError(SemanticError::UNINITIALIZED_FINAL_VARIABLE_IN_CONSTRUCTOR,
  2199.                                        constructor_decl -> LeftToken(),
  2200.                                        constructor_decl -> RightToken(),
  2201.                                        variable_symbol -> Name());
  2202.                     }
  2203.                 }
  2204.             }
  2205.         }
  2206.  
  2207.         for (int l = 0; l < this_type -> NumPrivateAccessConstructors(); l++)
  2208.         {
  2209.             ThisMethod() = this_type -> PrivateAccessConstructor(l);
  2210.             MethodSymbol *this_method = ThisMethod();
  2211.             AstConstructorDeclaration *constructor_decl = (AstConstructorDeclaration *)
  2212.                                                           this_method -> method_or_constructor_declaration;
  2213.  
  2214.             LocalSymbolTable().Push(this_method -> block_symbol -> Table());
  2215.             LocalBlockStack().max_size = 0;
  2216.             ProcessConstructorBody(constructor_decl, true);
  2217.             LocalSymbolTable().Pop();
  2218.             this_method -> max_block_depth = LocalBlockStack().max_size;
  2219.         }
  2220.  
  2221.         ConstructorCycleChecker cycle_checker(class_body);
  2222.     }
  2223.  
  2224.     for (int j = 0; j < class_body -> NumMethods(); j++)
  2225.     {
  2226.         AstMethodDeclaration *method_decl = class_body -> Method(j);
  2227.  
  2228.         ThisMethod() = method_decl -> method_symbol;
  2229.         MethodSymbol *this_method = ThisMethod();
  2230.         if (this_method)
  2231.         {
  2232.             //
  2233.             // TODO: Confirm that this new test is indeed necessary. In 1.0, a more restricted test was used...
  2234.             //
  2235.             for (int i = 0; i < this_method -> NumFormalParameters(); i++)
  2236.             {
  2237.                 VariableSymbol *parm = this_method -> FormalParameter(i);
  2238.                 AstVariableDeclaratorId *name = parm -> declarator -> variable_declarator_name;
  2239.  
  2240.                 SemanticEnvironment *where_found;
  2241.                 Tuple<VariableSymbol *> variables_found(2);
  2242.                 SearchForVariableInEnvironment(variables_found, where_found, state_stack.Top(),
  2243.                                                parm -> Identity(),
  2244.                                                name -> identifier_token);
  2245.                 VariableSymbol *symbol = (variables_found.Length() > 0 ? variables_found[0] : (VariableSymbol *) NULL);
  2246.                 if (symbol && symbol -> IsLocal())
  2247.                 {
  2248.                     ReportSemError(SemanticError::DUPLICATE_LOCAL_VARIABLE_DECLARATION,
  2249.                                    name -> identifier_token,
  2250.                                    name -> identifier_token,
  2251.                                    parm -> Name());
  2252.                 }
  2253.             }
  2254.  
  2255.             LocalSymbolTable().Push(this_method -> block_symbol -> Table());
  2256.             LocalBlockStack().max_size = 0;
  2257.  
  2258.             int start_num_errors = NumErrors();
  2259.             ProcessMethodBody(method_decl);
  2260.  
  2261.             LocalSymbolTable().Pop();
  2262.             this_method -> max_block_depth = LocalBlockStack().max_size;
  2263.  
  2264.             if (NumErrors() == start_num_errors)
  2265.                 DefiniteMethodBody(method_decl, finals);
  2266.         }
  2267.     }
  2268.  
  2269.     //
  2270.     // Mark all instance variables and constructor parameters final.
  2271.     //
  2272.     for (int i = 0; i <  this_type -> NumConstructorParameters(); i++)
  2273.         this_type -> ConstructorParameter(i) -> SetACC_FINAL();
  2274.  
  2275.     for (int l = 0; l <  this_type -> NumEnclosingInstances(); l++)
  2276.         this_type -> EnclosingInstance(l) -> SetACC_FINAL();
  2277.  
  2278.     //
  2279.     // We are done with all the methods, indicate that there is no method
  2280.     // being currently compiled in this environment.
  2281.     //
  2282.     ThisMethod() = NULL;
  2283.  
  2284.     //
  2285.     // Recursively process all inner types
  2286.     //
  2287.     for (int m = 0; m < class_body -> NumNestedClasses(); m++)
  2288.     {
  2289.         AstClassDeclaration *class_declaration = class_body -> NestedClass(m);
  2290.         if (class_declaration -> semantic_environment)
  2291.             ProcessExecutableBodies(class_declaration -> semantic_environment, class_declaration -> class_body);
  2292.     }
  2293.  
  2294.     for (int n = 0; n < class_body -> NumNestedInterfaces(); n++)
  2295.     {
  2296.         if (class_body -> NestedInterface(n) -> semantic_environment)
  2297.             ProcessExecutableBodies(class_body -> NestedInterface(n));
  2298.     }
  2299.  
  2300.     state_stack.Pop();
  2301.  
  2302.     return;
  2303. }
  2304.  
  2305.  
  2306. void Semantic::ProcessExecutableBodies(AstInterfaceDeclaration *interface_declaration)
  2307. {
  2308.     state_stack.Push(interface_declaration -> semantic_environment);
  2309.     TypeSymbol *this_type = ThisType();
  2310.  
  2311.     assert(this_type -> HeaderProcessed());
  2312.     assert(this_type -> MethodMembersProcessed());
  2313.     assert(this_type -> FieldMembersProcessed());
  2314.  
  2315.     for (int k = 0; k < this_type -> NumVariableSymbols(); k++)
  2316.     {
  2317.         VariableSymbol *variable_symbol = this_type -> VariableSym(k);
  2318.         if (! variable_symbol -> IsDefinitelyAssigned())
  2319.         {
  2320.             ReportSemError(SemanticError::UNINITIALIZED_FINAL_VARIABLE,
  2321.                            variable_symbol -> declarator -> LeftToken(),
  2322.                            variable_symbol -> declarator -> RightToken());
  2323.         }
  2324.     }
  2325.  
  2326.     //
  2327.     // Recursively process all inner types
  2328.     //
  2329.     for (int m = 0; m < interface_declaration -> NumNestedClasses(); m++)
  2330.     {
  2331.         AstClassDeclaration *class_declaration = interface_declaration -> NestedClass(m);
  2332.         if (class_declaration -> semantic_environment)
  2333.             ProcessExecutableBodies(class_declaration -> semantic_environment, class_declaration -> class_body);
  2334.     }
  2335.  
  2336.     for (int n = 0; n < interface_declaration -> NumNestedInterfaces(); n++)
  2337.     {
  2338.         if (interface_declaration -> NestedInterface(n) -> semantic_environment)
  2339.             ProcessExecutableBodies(interface_declaration -> NestedInterface(n));
  2340.     }
  2341.  
  2342.     state_stack.Pop();
  2343.  
  2344.     return;
  2345. }
  2346.